使用了RecyclerVIEw嵌套RecyclerVIEw的方案。
购物车的第一个界面为RecyclerVIEw,每个Item里面包含一个店铺。在Item中使用RecyclerVIEw包含店铺和店铺的多个商品。
@H_404_6@实现思路:
使用接口回调将第二个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 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,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的嵌套选择的逻辑全部内容,希望文章能够帮你解决Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)