android - RecyclerView with checkbox checks all items in subcategories of same index value -
i have recyclerview lists number of items , clicking on1 item opens dialog subcategories of item.each subcategory has checkbpx it.the problem when click on 1 checkbox , click ok , go select category item of same index in subcategory checked.this code dialog box lists subcategories
@override public void onclick(view view) { switch (view.getid()) { case r.id.bt_ok: // updatecatagory(); (int = 0; < mysubcatagoryadapter.catagory_list.size(); i++) { if (!mysubcatagoryadapter.ischecked) { // mysubcatagoryadapter.removehash.get(i).; //mysubcatagoryadapter.removehash.clear(); mysubcatagoryadapter.removehash.remove(i); } } finish(); } }
this adapter
public class customrecycleviewadapter_language extends recyclerview.adapter<customrecycleviewadapter_language.mylanguageviewholder> { private context context; public arraylist<string> catagory_list; private view resourcelayout; private onselected onselected; private string checkforaction; public arraylist<string> removehash; public boolean ischecked=false; int count = 1; sharedpreferences selection = getapplicationcontext().getsharedpreferences("mylang", mode_private); sharedpreferences.editor editor = selection.edit(); public customrecycleviewadapter_language(context context, arraylist<string> catagory_list, string checkforaction) { this.context = context; this.catagory_list = catagory_list; this.checkforaction = checkforaction; } public class mylanguageviewholder extends recyclerview.viewholder { private textview tv_option; private checkbox cb_option; private relativelayout rl_option; public mylanguageviewholder(view itemview) { super(itemview); tv_option = (textview) itemview.findviewbyid(r.id.tv_option); cb_option = (checkbox) itemview.findviewbyid(r.id.cb_option); rl_option = (relativelayout) itemview.findviewbyid(r.id.rl_option); } } @override public mylanguageviewholder oncreateviewholder(viewgroup parent, int viewtype) { resourcelayout = layoutinflater.from(parent.getcontext()).inflate(r.layout.resource_selectfilter, null); mylanguageviewholder mcv = new mylanguageviewholder(resourcelayout); return mcv; } @override public void onbindviewholder(final mylanguageviewholder holder, final int position) { holder.tv_option.settext(catagory_list.get(position).split("#") [1].substring(0, 1).touppercase() + catagory_list.get(position).split("#")[1].substring(1)); removehash = new arraylist<>(); removehash.clear(); common.languageselection.add(position); (int = 0; < catagory_list.size(); i++) { removehash.add(catagory_list.get(i).split("#")[0]); } if (!common.isflag) { try { if(common.languageselection.size()!=0) { if (common.setid.get(position).equals(string.valueof(position))) { holder.cb_option.setchecked(true); } } count = 2; } catch (exception e) { } } if (checkforaction.equals("upload")) { holder.cb_option.setvisibility(view.gone); } holder.rl_option.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (onselected != null) { if (count == 2) { if (!holder.cb_option.ischecked()) { holder.cb_option.setchecked(true); removehash.get(position); ischecked=true; common.setid.add(string.valueof(position)); } else if (holder.cb_option.ischecked()) { holder.cb_option.setchecked(false); common.setid.remove(string.valueof(position)); removehash.remove(position); } onselected.onclicked(view, removehash.get(position)); ischecked=true; } else { if (!holder.cb_option.ischecked()) { holder.cb_option.setchecked(true); common.setid.add(string.valueof(position)); removehash.get(position); ischecked=true; } else if (holder.cb_option.ischecked()) { holder.cb_option.setchecked(false); removehash.remove(position); common.setid.remove(string.valueof(position)); } if (checkforaction.equals("upload")) { holder.cb_option.setvisibility(view.gone); onselected.onclicked(view, catagory_list.get(position)); } else onselected.onclicked(view, removehash.get(position)); common.holdvalues.add(removehash.get(position)); } log.v("show", removehash.tostring()); } } }); // try // { // // if(common.languageselection.size()>0) // { // for(int i=0;i<common.languageselection.size();i++) // { // // // if(common.languageselection.get(i).equals(common.setid.get(i))) // { // // holder.cb_option.setchecked(true); // } // else // holder.cb_option.setchecked(false); // // } // } // // }catch (exception e) // { // // } /* holder.tv_option.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if (onlanguageselected != null) { onlanguageselected.onlanguageclicked(view,catagory_list.get(position)); } } });*/ } @override public int getitemcount() { return catagory_list.size(); } public void setonselected(onselected onselected) { this.onselected = onselected; } public interface onselected { public void onclicked(view view, string selectedval); } }
this layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/white"> <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true"> <android.support.v7.widget.recyclerview android:id="@+id/rv_subcatagories" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="2dp" android:padding="20dp" android:scrollbars="vertical"> </android.support.v7.widget.recyclerview> <linearlayout android:id="@+id/ll_respondcontainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_below="@+id/rv_subcatagories" android:orientation="horizontal" android:padding="10dp"> <button android:id="@+id/bt_ok" android:layout_width="80dp" android:layout_height="30dp" android:layout_marginright="2dp" android:background="@drawable/btn_background" android:text="ok" android:textcolor="@color/white" /> </linearlayout> </relativelayout> </relativelayout>
recyclerview items
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingright="3dp" android:paddingtop="2dp"> <relativelayout android:id="@+id/rl_option" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:orientation="horizontal" android:padding="5dp" android:layout_centervertical="true" android:layout_centerhorizontal="true"> <checkbox android:id="@+id/cb_option" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:clickable="false" android:button="@drawable/checkbox_selector" android:gravity="left" /> <textview android:id="@+id/tv_option" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_centervertical="true" android:text="arindom" android:background="@drawable/edittext_bg" android:gravity="center" android:padding="2dp" android:textcolor="@color/filtertextcolor" android:textsize="15dp" /> <view android:id="@+id/underpassword" android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/tv_option" android:layout_centerhorizontal="true" android:background="@color/linecolor" /> </relativelayout> </relativelayout>
from question made example pls check if helps.
main class recyclerview activity
public class recyclerviewactivity extends appcompatactivity implements categoryclicklistener { private recyclerview recyclerviewcategory; private recyclerview.layoutmanager mlayoutmanager; private context context = recyclerviewactivity.this; private activity activity = recyclerviewactivity.this; private categoryadapter categoryadapter; private categoryclicklistener categoryclicklistener; arraylist<arraylist<subcategorymodel>> listdata = new arraylist<>(); @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recycler_view); categoryclicklistener = this; getdummydata(); recyclerviewcategory = (recyclerview) findviewbyid(r.id.recycler_view); mlayoutmanager = new linearlayoutmanager(context); recyclerviewcategory.sethasfixedsize(true); recyclerviewcategory.setlayoutmanager(mlayoutmanager); categoryadapter = new categoryadapter(context, listdata, categoryclicklistener); recyclerviewcategory.setadapter(categoryadapter); } public void getdummydata() { (int = 0; < 10; i++) { arraylist<subcategorymodel> subcategorydata = new arraylist<>(); subcategorydata.add(new subcategorymodel("sub category 1", false)); subcategorydata.add(new subcategorymodel("sub category 2", false)); subcategorydata.add(new subcategorymodel("sub category 3", false)); subcategorydata.add(new subcategorymodel("sub category 4", false)); subcategorydata.add(new subcategorymodel("sub category 5", false)); listdata.add(subcategorydata); } } private void showdialog(arraylist<subcategorymodel> listsubcategory) { try { final dialog dialog = new dialog(context); dialog.setcancelable(true); dialog.requestwindowfeature(window.feature_no_title); dialog.getwindow().getattributes().windowanimations = r.style.animation_appcompat_dropdownup; dialog.setcontentview(r.layout.dialog_subcategory); windowmanager.layoutparams lp = new windowmanager.layoutparams(); window window = dialog.getwindow(); lp.copyfrom(window.getattributes()); lp.width = windowmanager.layoutparams.match_parent; lp.height = windowmanager.layoutparams.wrap_content; window.setattributes(lp); recyclerview recyclerviewsubcategory = (recyclerview) dialog.findviewbyid(r.id.recycler_view_subcategory); recyclerview.layoutmanager mlayoutmanager = new linearlayoutmanager(context); recyclerviewsubcategory.sethasfixedsize(true); recyclerviewsubcategory.setlayoutmanager(mlayoutmanager); subcategoryadapter adapter = new subcategoryadapter(context, listsubcategory); recyclerviewsubcategory.setadapter(adapter); dialog.show(); } catch (exception e) { e.printstacktrace(); } } @override public void categoryclick(int position) { showdialog(listdata.get(position)); } }
category adapter class
public class categoryadapter extends recyclerview.adapter<categoryadapter.customviewholder> { private arraylist<arraylist<subcategorymodel>> listdata; private context context; private categoryclicklistener categoryclicklistener; public categoryadapter(context context, arraylist<arraylist<subcategorymodel>> listdata, categoryclicklistener categoryclicklistener) { this.listdata = listdata; this.context = context; this.categoryclicklistener = categoryclicklistener; } @override public categoryadapter.customviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.row_category_view, parent, false); categoryadapter.customviewholder viewholder = new categoryadapter.customviewholder(view); return viewholder; } //this method binds widgets value @override public void onbindviewholder(final categoryadapter.customviewholder holder, int position) { try { holder.textviewcategory.settext("category "+(position+1)); } catch (exception e) { e.printstacktrace(); } } @override public int getitemcount() { return (null != listdata ? listdata.size() : 0); } class customviewholder extends recyclerview.viewholder implements view.onclicklistener { private textview textviewcategory; public customviewholder(view itemview) { super(itemview); itemview.setonclicklistener(this); textviewcategory = (textview) itemview.findviewbyid(r.id.textviewcategory); } @override public void onclick(view v) { categoryclicklistener.categoryclick(getadapterposition()); } } }
subcategory adapter class
class subcategoryadapter extends recyclerview.adapter<subcategoryadapter.customviewholder> { private arraylist<subcategorymodel> listdata; private context context; public subcategoryadapter(context context, arraylist<subcategorymodel> listdata) { this.listdata = listdata; this.context = context; } @override public subcategoryadapter.customviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.row_subcategory_view, parent, false); subcategoryadapter.customviewholder viewholder = new subcategoryadapter.customviewholder(view); return viewholder; } //this method binds widgets value @override public void onbindviewholder(final subcategoryadapter.customviewholder holder, int position) { try { subcategorymodel subcategory = listdata.get(position); holder.textviewsubcategory.settext(subcategory.name); if(subcategory.ischecked) { holder.checkbox.setchecked(true); }else{ holder.checkbox.setchecked(false); } } catch (exception e) { e.printstacktrace(); } } @override public int getitemcount() { return (null != listdata ? listdata.size() : 0); } class customviewholder extends recyclerview.viewholder { private textview textviewsubcategory; private checkbox checkbox; public customviewholder(view itemview) { super(itemview); textviewsubcategory = (textview) itemview.findviewbyid(r.id.textviewsubcategory); checkbox = (checkbox) itemview.findviewbyid(r.id.checkbox); checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { listdata.get(getadapterposition()).ischecked=ischecked; } }); } } }
subcategory model class
public class subcategorymodel { string name; boolean ischecked; public subcategorymodel(string name, boolean ischecked) { this.name = name; this.ischecked = ischecked; } }
category click listener
public interface categoryclicklistener { public void categoryclick(int position); }
activity_recycler_view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:scrollbars="none"> </android.support.v7.widget.recyclerview> </linearlayout>
dialog_subcategory
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view_subcategory" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:scrollbars="none"> </android.support.v7.widget.recyclerview> </linearlayout>
row_category_view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:padding="10dp"> <textview android:id="@+id/textviewcategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000"/> </linearlayout>
row_subcategory_view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <checkbox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/textviewsubcategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000"/> </linearlayout>
it may you.
Comments
Post a Comment