The logic of nested selection of Taobao shopping cart Recyclerview or listview in Android _android

Source: Internet
Author: User
Tags call back gettext static class recyclerview android

A scenario with Recyclerview nested Recyclerview is used.

The first interface of the shopping cart is Recyclerview, and each item contains a store. Use Recyclerview in the item to include multiple items in a store or shop.

Realize the idea:

Use an interface callback to callback the listener event for a second adapter product selection to the first adapter and then back to mainactivity in the first adapter.

Use an interface callback to callback the listener event for the first adapter item to mainactivity.

Handle the first adapter and second adapter event listening in mainactivity.

Mainactivity:

public class Mainactivity extends Appcompatactivity {private Recyclerview recyclerview; 
private checkbox checkbox; 
Private Recycleradapter adapter; 
Private Recyclerview.layoutmanager Manager; 
Private list<bean> List; 
Private list<cbean> cbeanlist,cbeanlistcp; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
Recyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview); 
checkbox = (checkbox) Findviewbyid (R.id.shop_checkbox); 
List = new arraylist<> (); 
The first store data cbeanlist = new arraylist<> (); 
Cbean C = new Cbean (); 
C.settext ("commodity"); 
C.setischeck (FALSE); 
Cbean C1 = new Cbean (); 
C1.settext ("Product 1"); 
C1.setischeck (FALSE); 
Cbeanlist.add (c); 
Cbeanlist.add (C1); 
Bean B = new Bean (); 
B.setischeck (FALSE); 
B.settext ("Shop name"); 
B.setlist (cbeanlist); 
The second store data cbeanlistcp = new arraylist<> (); 
Cbean C2 = new Cbean (); 
C2.settext ("Product 2"); C2.setischeck (False); 
Cbean C3 = New Cbean (); 
C3.settext ("Product 3"); 
C3.setischeck (FALSE); 
Cbeanlistcp.add (C2); 
Cbeanlistcp.add (C3); 
Bean B1 = new Bean (); 
B1.setischeck (FALSE); 
B1.settext ("Shop name 1"); 
B1.setlist (CBEANLISTCP); 
Cannot add data with duplicate variables List.add (b); 
List.add (B1); 
Manager = new Linearlayoutmanager (this); 
Recyclerview.setlayoutmanager (manager); 
Optimize performance recyclerview.sethasfixedsize (true); 
adapter = new Recycleradapter (list); 
Recyclerview.setadapter (adapter); Select All CheckBox listener Checkbox.setoncheckedchangelistener (new Compoundbutton.oncheckedchangelistener () {@Override public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {if (ischecked) {for (int i = 0;i < List.size (); 
i++) {//select Shop if (!list.get (i) Ischeck ()) {List.get (i). Setischeck (True); for (int j = 0;j < List.get (i). GetList (). Size (); j + +) {//Select the store's product if (!list.get (i). GetList (). Get (J). Ischeck ()) {lis 
T.get (i). GetList (). Get (J). Setischeck (True); }} {//}else {///only when clicked is not selected to perform//Resolve click cancel Select shop or merchandise,//All Select button CancelSelect the state, will not be invariant optional if (allselect () = List.size ()) {for (int i = 0;i < List.size (); i++) {if List.get (i). Ischeck ()) {lis 
T.get (i). Setischeck (false); for (int j = 0;j < List.get (i). GetList (). Size (); j + +) {if list.get (i). GetList (). Get (J). Ischeck ()) {List.get (i) 
List (). Get (J). Setischeck (false); 
}}}//update Updaterecyclerview (); 
} 
}); Adapter.setcallback (New Recycleradapter.allcheck () {@Override public void Onchecklistener (boolean isselected, int posi 
tion) {//Save store Click Status List.get (Position). Setischeck (isselected); 
Notifies the select state of the selected CheckBox if (allselect () = = List.size ()) {checkbox.setchecked (true); 
}else {checkbox.setchecked (false); } if (isselected) {for int i = 0;i < List.get (position). GetList (). Size (); i++) {if!list.get (position). GetList (). GE 
T (i). Ischeck ()) {list.get (position). GetList (). get (i). Setischeck (True); }}else {//Resolve click to deselect the product,//Shop Select button deselect the state, will not be the same as not optional if (allchildselect (position) = = List.get (position). GetList () siz E ()) {for (int i = 0;i < List.get (position). GetList (). Size (); i++) {if list.get (position). GetList (). Get (i) Ischeck ()) {List.get ( 
Position). GetList (). get (i). Setischeck (false); 
}}//update Updaterecyclerview (); @Override public void Onitemchecklistener (boolean isselected, int parentposition, int chaildposition) {//Save item Click Status L 
Ist.get (parentposition). GetList (). Get (Chaildposition). Setischeck (isselected); Notify the store to select the status if (Allchildselect (parentposition) = = List.get (parentposition). GetList (). Size ()) {List.get ( 
parentposition). Setischeck (True); 
}else {list.get (parentposition). Setischeck (false); 
} updaterecyclerview (); 
} 
}); 
/* * Resolve Recycleyview Refresh error problem/private void Updaterecyclerview () {Handler Handler = new Handler (); 
Final Runnable r = new Runnable () {public void run () {adapter.notifydatasetchanged (); 
} 
}; 
Handler.post (R); 
///Calculate the number of shops selected private int allselect () {int sum = 0; 
for (int i = 0; i < list.size (); i++) {if (List.get (i). Ischeck ()) {sum++; } SySTEM.OUT.PRINTLN (sum); 
return sum; 
//Calculate the selected number of items per store private int allchildselect (int position) {int sum = 0; for (int i = 0; i < list.get (position). GetList (). Size (); i++) {if list.get (position). GetList (). Get (i) Ischeck ()) {s 
um++; 
System.out.println (position+ ":" +i+ ":" +list.get (position). GetList (). Get (i) Ischeck ()); 
return sum;  } 
}

First adapter:

public class Recycleradapter extends recyclerview.adapter<recycleradapter.myholder> {private list<bean> 
List 
Public Recycleradapter (list<bean> list) {this.list = list; 
public static class Myholder extends recyclerview.viewholder{private Recyclerview recyclerview; 
Private TextView TextView; 
private checkbox checkbox; 
Private RecyclerAdapter1 adapter; 
Private Recyclerview.layoutmanager Manager; 
Public checkbox Getcheckbox () {return checkbox; 
Public Recyclerview Getrecyclerview () {return recyclerview; 
Public TextView Gettextview () {return TextView; 
Public Myholder (View Itemview) {super (Itemview); 
Recyclerview = (Recyclerview) Itemview.findviewbyid (R.id.list_items); 
TextView = (TextView) Itemview.findviewbyid (r.id.tv_name); 
checkbox = (checkbox) Itemview.findviewbyid (r.id.checkbox0); 
Manager = new Linearlayoutmanager (Itemview.getcontext ()); 
Recyclerview.setlayoutmanager (manager); }} @Override public Myholder OncreateviewholdER (viewgroup parent, int viewtype) {View view = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.shop_item, 
NULL); 
Myholder holder = new Myholder (view); 
return holder; @Override public void Onbindviewholder (final myholder holder, final int position) {Holder.adapter = new Recycleradap 
Ter1 (List.get (position). GetList ()); 
Holder.recyclerView.setAdapter (Holder.adapter); 
Holder.gettextview (). SetText (List.get (position). GetText ()); 
Holder.getcheckbox (). setchecked (List.get (position). Ischeck ()); Holder.getcheckbox (). Setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {//Call back the click-Change event for the checkbox in the shop if (Mcallback!=null) {m 
Callback.onchecklistener (ischecked,position); 
} 
} 
}); The callback interface that implements the second-level Recyclerview holder.adapter.setCallBack (new Recycleradapter1.allcheck () {@Override public void Onchecklistener (boolean ischecked, int childpostion) {//click-Change event for CheckBox of store MerchandiseCallback if (mcallback!=null) {Mcallback.onitemchecklistener (ischecked,position,childpostion); 
} 
} 
}); 
Holder.itemView.setTag (List.get (position)); 
@Override public int GetItemCount () {return list.size (); 
Private Allcheck Mcallback; 
public void Setcallback (Allcheck callBack) {mcallback = CallBack; The public interface allcheck{//callback function recalls the click Change event for the checkbox in the store to public void Onchecklistener (Boolean isselected,int position 
); The callback function recalls the click-Change event for the checkbox of the store's merchandise to the public void Onitemchecklistener (Boolean isselected,int parentposition,int 
Chaildposition);  } 
}

Second adapter:

public class RecyclerAdapter1 extends recyclerview.adapter<recycleradapter1.myholder> {private List<cbean 
> Cbeanlist, CbeanList1; 
Public RecyclerAdapter1 (list<cbean> cbeanlist) {this.cbeanlist = cbeanlist; 
CbeanList1 = cbeanlist; 
public static class Myholder extends Recyclerview.viewholder {private TextView TextView; 

private checkbox checkbox; 
Public TextView Gettextview () {return TextView; 
Public checkbox Getcheckbox () {return checkbox; 
Public Myholder (View Itemview) {super (Itemview); 
TextView = (TextView) Itemview.findviewbyid (R.ID.CHECKBOX_TV); 
checkbox = (checkbox) Itemview.findviewbyid (R.id.checkbox1); } @Override Public Myholder oncreateviewholder (viewgroup parent, int viewtype) {View view = Layoutinflater.from (par 
Ent.getcontext ()). Inflate (R.layout.check_item, NULL); 
Myholder holder = new Myholder (view); 
return holder; @Override public void Onbindviewholder (final myholder holder, final int position) {Holder.gettexTview (). SetText (Cbeanlist.get (position). GetText ()); 
Holder.getcheckbox (). setchecked (Cbeanlist.get (position). Ischeck ()); Holder.getcheckbox (). Setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {//callback the click-Change event of the item's checkbox to the first Recyclerview if ( 
Mcallback!= null) {Mcallback.onchecklistener (ischecked, position); 
} 
} 
}); 
Holder.itemView.setId (position); 
@Override public int GetItemCount () {return cbeanlist.size (); 
Private Allcheck Mcallback; 
public void Setcallback (Allcheck callBack) {mcallback = CallBack; The public interface Allcheck {//callback function recalls the click-Change event for the checkbox of the store merchandise to the public void Onchecklistener (Boolean ischecked, int child 
postion); } 
}

Entity classes save data and select state:

public class Bean { 
private boolean ischeck; 
private String text; 
Private list<cbean> List; 
public Boolean Ischeck () {return 
ischeck; 
} 
public void Setischeck (Boolean ischeck) { 
this.ischeck = Ischeck; 
} 
Public String GetText () {return 
text; 
} 
public void SetText (String text) { 
this.text = text; 
} 
Public list<cbean> GetList () {return 
List; 
} 
public void setlist (list<cbean> list) { 
this.list = list; 
} 
} 
public class Cbean { 
private boolean ischeck; 
private String text; 
public Boolean Ischeck () {return 
ischeck; 
} 
public void Setischeck (Boolean ischeck) { 
this.ischeck = Ischeck; 
} 
Public String GetText () {return 
text; 
} 
public void SetText (String text) { 
this.text = text; 
} 
}

Layout file: Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_main "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "tools:context=" Com.example.cuboo.myapplication.MainActivity "> <android.support.v7.widget.recyclerview android:id=" @+id/ Recyclerview "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:layout_weight=" "1" > </android.support.v7.widget.RecyclerView> <linearlayout android:layout_width= "Match_parent" Android: 
layout_height= "Wrap_content" > <checkbox android:id= "@+id/shop_checkbox" android:layout_marginleft= "12DP" Android:layout_width= "24DP" android:layout_height= "24DP" android:layout_gravity= "Left|center" android:padding= "  12DP "android:gravity=" center "/> </LinearLayout> </LinearLayout>

Shop_item.xml:

<?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=" Match_parent " 
> <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <CheckBox Android:id= "@+id/checkbox0" android:layout_width= "24DP" android:layout_height= "24DP"/> <textview-android:id= "@+id/tv_name" android:text= "Shop name" android:gravity= "center" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content"/> </LinearLayout> <view android:layout_width= "Match_parent" Android:layout_ height= "0.5DP" android:background= "@color/coloraccent"/> <android.support.v7.widget.recyclerview android:id = "@+id/list_items" android:layout_width= "match_parent" android:layout_height= "wrap_content" > </ android.support.v7.widget.recyclerview> <view android:layout_width= "match_parent"Android:layout_height=" 48DP "android:background=" @color/coloraccent "/> </LinearLayout>  

Check_item:

<?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= "60DP" > 
<checkbox 
android:layout_gravity= "center" 
android:id= "@+id/checkbox1" 
Android:layout_width= "24DP" 
android:layout_height= "24DP"/> 
<textview 
android:id= "@+id/" Checkbox_tv " 
android:text=" 222 " 
android:layout_weight=" 1 " 
android:layout_gravity=" center " 
android:gravity= "center" 
android:layout_width= "wrap_content" 
android:layout_height= "wrap_content"/ > 

Simple Effect Chart:

The above is a small set of Android to introduce the realization of Taobao shopping cart Recyclerview or ListView of the logic of the nested choice, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.