使用了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 Listlist; private List 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
第一个Adapter:
public class recyclerAdapter extends RecyclerView.Adapter{ private List list; public recyclerAdapter(List 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{ private List cbeanList, cbeanList1; public recyclerAdapter1(List 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 Listlist; 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 getList() { return list; } public void setList(List 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
<&#63;xml version="1.0" encoding="utf-8"&#63;>
shop_item.xml:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
check_item:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
简单的效果图:
以上所述是小编给大家介绍的Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!