欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑

程序员文章站 2024-02-29 08:33:10
使用了recyclerview嵌套recyclerview的方案。 购物车的第一个界面为recyclerview,每个item里面包含一个店铺。在item中使用recyc...

使用了recyclerview嵌套recyclerview的方案。

购物车的第一个界面为recyclerview,每个item里面包含一个店铺。在item中使用recyclerview包含店铺和店铺的多个商品。

实现思路:

使用接口回调将第二个adapter的商品选择的监听事件回调给第一个adapter后再在第一个adapter中回调给mainactivity。

使用接口回调将第一个adapter的商品选择的监听事件回调给mainactivity。

在mainactivity中处理第一个adapter和第二个adapter的事件监听。

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<>(); 
//第一个店铺的数据 
cbeanlist = new arraylist<>(); 
cbean c = new cbean(); 
c.settext("商品"); 
c.setischeck(false); 
cbean c1 = new cbean(); 
c1.settext("商品1"); 
c1.setischeck(false); 
cbeanlist.add(c); 
cbeanlist.add(c1); 
bean b = new bean(); 
b.setischeck(false); 
b.settext("店名"); 
b.setlist(cbeanlist); 
//第二个店铺的数据 
cbeanlistcp = new arraylist<>(); 
cbean c2 = new cbean(); 
c2.settext("商品2"); 
c2.setischeck(false); 
cbean c3 = new cbean(); 
c3.settext("商品3"); 
c3.setischeck(false); 
cbeanlistcp.add(c2); 
cbeanlistcp.add(c3); 
bean b1 = new bean(); 
b1.setischeck(false); 
b1.settext("店名1"); 
b1.setlist(cbeanlistcp); 
//不能添加有重复变量的数据 
list.add(b); 
list.add(b1); 
manager = new linearlayoutmanager(this); 
recyclerview.setlayoutmanager(manager); 
//优化性能 
recyclerview.sethasfixedsize(true); 
adapter = new recycleradapter(list); 
recyclerview.setadapter(adapter); 
//全选checkbox监听 
checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { 
@override 
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { 
if (ischecked){ 
for (int i = 0;i < list.size();i++){ 
//选择店铺 
if (!list.get(i).ischeck()){ 
list.get(i).setischeck(true); 
} 
for (int j = 0;j < list.get(i).getlist().size();j++){ 
//选择店铺的商品 
if (!list.get(i).getlist().get(j).ischeck()){ 
list.get(i).getlist().get(j).setischeck(true); 
} 
} 
} 
}else { 
//只有当点击全不选时才执行 
// 解决点击取消选择店铺或商品时, 
// 全选按钮取消选择状态,不会不变成全不选 
if (allselect() == list.size()){ 
for (int i = 0;i < list.size();i++){ 
if (list.get(i).ischeck()){ 
list.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).getlist().get(j).setischeck(false); 
} 
} 
} 
} 
} 
//更新 
updaterecyclerview(); 
} 
}); 
adapter.setcallback(new recycleradapter.allcheck() { 
@override 
public void onchecklistener(boolean isselected, int position) { 
//保存店铺点击状态 
list.get(position).setischeck(isselected); 
//通知全选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().get(i).ischeck()){ 
list.get(position).getlist().get(i).setischeck(true); 
} 
} 
}else { 
// 解决点击取消选择商品时, 
// 店铺全选按钮取消选择状态,不会不变成全不选 
if (allchildselect(position) == list.get(position).getlist().size()){ 
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); 
} 
} 
} 
} 
//更新 
updaterecyclerview(); 
} 
@override 
public void onitemchecklistener(boolean isselected, int parentposition, int chaildposition) { 
//保存商品点击状态 
list.get(parentposition).getlist().get(chaildposition).setischeck(isselected); 
//通知店铺选择的状态 
if (allchildselect(parentposition) == list.get(parentposition).getlist().size()){ 
list.get(parentposition).setischeck(true); 
}else { 
list.get(parentposition).setischeck(false); 
} 
updaterecyclerview(); 
} 
}); 
} 
/* 
*解决recycleyview刷新报错问题 
*/ 
private void updaterecyclerview() { 
handler handler = new handler(); 
final runnable r = new runnable() { 
public void run() { 
adapter.notifydatasetchanged(); 
} 
}; 
handler.post(r); 
} 
//计算店铺的选择数量 
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; 
} 
//计算每个店铺商品的选择数量 
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()){ 
sum++; 
system.out.println(position+":"+i+":"+list.get(position).getlist().get(i).ischeck()); 
} 
} 
return sum; 
} 
} 

第一个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 recycleradapter1(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) { 
//将店铺的checkbox的点击变化事件进行回调 
if (mcallback!=null){ 
mcallback.onchecklistener(ischecked,position); 
} 
} 
}); 
//实现第二层recyclerview的回调接口 
holder.adapter.setcallback(new recycleradapter1.allcheck() { 
@override 
public void onchecklistener(boolean ischecked, int childpostion) { 
//将店铺商品的checkbox的点击变化事件进行回调 
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; 
} 
public interface allcheck{ 
//回调函数 将店铺的checkbox的点击变化事件进行回调 
public void onchecklistener(boolean isselected,int position); 
//回调函数 将店铺商品的checkbox的点击变化事件进行回调 
public void onitemchecklistener(boolean isselected,int parentposition,int chaildposition); 
} 
} 

第二个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(parent.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) { 
//将商品的checkbox的点击变化事件进行回调给第一个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; 
} 
public interface allcheck { 
//回调函数 将店铺商品的checkbox的点击变化事件进行回调 
public void onchecklistener(boolean ischecked, int childpostion); 
} 
}

实体类保存数据和选择状态:

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; 
} 
}

布局文件: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="店名" 
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" /> 
</linearlayout> 

简单的效果图:

Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑

以上所述是小编给大家介绍的android中实现淘宝购物车recyclerview或listview的嵌套选择的逻辑,希望对大家有所帮助