i have implemented recyclerview adapter items , each item has drawer item layout containing imageview , textview.
problem onclick function called when image or "actual text" differs in size clicked , not whole row.
there way make whole row listen onclick function? please let me know if have ideas. in advance.
recyclerview image
recyclerviewadapter.java
public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.viewholder> { string[] titles; typedarray icons; context context; recyclerviewadapter(string[] titles, typedarray icons, context context){ this.titles = titles; this.icons = icons; this.context = context; } public class viewholder extends recyclerview.viewholder implements view.onclicklistener { textview navtitle; imageview navicon; context context; public viewholder(view draweritem , int itemtype , context context){ super(draweritem); this.context = context; draweritem.setonclicklistener(this); if(itemtype==1){ navtitle = (textview) itemview.findviewbyid(r.id.tv_navtitle); navicon = (imageview) itemview.findviewbyid(r.id.iv_navicon); } } /** *this defines onclick every item respect position. */ @override public void onclick(view v) { mainactivity mainactivity = (mainactivity)context; mainactivity.drawerlayout.closedrawers(); fragmenttransaction fragmenttransaction = mainactivity.getsupportfragmentmanager().begintransaction(); //switch (getposition()){ switch (getadapterposition()){ case 1: fragment fragment = new countrywise(); fragmenttransaction.replace(r.id.containerview,fragment); fragmenttransaction.commit(); break; case 2: fragment f2 = new companywise(); fragmenttransaction.replace(r.id.containerview,f2); fragmenttransaction.commit(); break; case 3: fragment = new about(); fragmenttransaction.replace(r.id.containerview,about); fragmenttransaction.commit(); break; } } } @override public recyclerviewadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater layoutinflater = (layoutinflater) parent.getcontext().getsystemservice(context.layout_inflater_service); if(viewtype==1){ view itemlayout = layoutinflater.inflate(r.layout.drawer_item_layout,null); return new viewholder(itemlayout,viewtype,context); } else if (viewtype==0) { view itemheader = layoutinflater.inflate(r.layout.header_layout,null); return new viewholder(itemheader,viewtype,context); } return null; } /** *this method called recyclerview.adapter display data @ specified position.� *this method should update contents of itemview reflect item @ given position. *so here , if position!=0 implies row_item , set title , icon of view. */ @override public void onbindviewholder(recyclerviewadapter.viewholder holder, int position) { if(position!=0){ holder.navtitle.settext(titles[position - 1]); holder.navicon.setimageresource(icons.getresourceid(position-1,-1)); } } @override public int getitemviewtype(int position) { if(position==0)return 0; else return 1; }
}
drawer_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:paddingbottom="8dp" android:paddingtop="8dp" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="16dp" android:id="@+id/iv_navicon" /> <textview android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="30dp" android:id="@+id/tv_navtitle" android:textappearance="?android:attr/textappearancemedium" android:textcolor="#000000" android:textsize="18sp" android:textstyle="bold" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawermainactivity" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:fitssystemwindows="true" android:minheight="?android:attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark" /> <framelayout android:id="@+id/containerview" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </framelayout> </linearlayout> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffff" android:paddingtop="40dp" android:scrollbars="vertical" /> </android.support.v4.widget.drawerlayout>
please let me know if need more code.
in adapter's oncreateviewholder()
method, change inflate()
calls follows.
if(viewtype == 1) { view itemlayout = layoutinflater.inflate(r.layout.drawer_item_layout, parent, false); return new viewholder(itemlayout, viewtype, context); } else if(viewtype == 0) { view itemheader = layoutinflater.inflate(r.layout.header_layout, parent, false); return new viewholder(itemheader, viewtype, context); }
your rows aren't matching recyclerview
's width, because you're not passing parent
inflater, it's wrapping width instead.
Comments
Post a Comment