Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
使用了RecyclerView嵌套RecyclerView的方案。
購物車的第一個界面為RecyclerView,每個Item里面包含一個店鋪。在Item中使用RecyclerView包含店鋪和店鋪的多個商品。
實現(xiàn)思路:
使用接口回調(diào)將第二個adapter的商品選擇的監(jiān)聽事件回調(diào)給第一個adapter后再在第一個adapter中回調(diào)給MainActivity。
使用接口回調(diào)將第一個adapter的商品選擇的監(jiān)聽事件回調(diào)給MainActivity。
在MainActivity中處理第一個adapter和第二個adapter的事件監(jiān)聽。
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<>();
//第一個店鋪的數(shù)據(jù)
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);
//第二個店鋪的數(shù)據(jù)
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);
//不能添加有重復(fù)變量的數(shù)據(jù)
list.add(b);
list.add(b1);
manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
//優(yōu)化性能
recyclerView.setHasFixedSize(true);
adapter = new recyclerAdapter(list);
recyclerView.setAdapter(adapter);
//全選CheckBox監(jiān)聽
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 {
//只有當(dāng)點擊全不選時才執(zhí)行
// 解決點擊取消選擇店鋪或商品時,
// 全選按鈕取消選擇狀態(tài),不會不變成全不選
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) {
//保存店鋪點擊狀態(tài)
list.get(position).setIscheck(isSelected);
//通知全選CheckBox的選擇狀態(tài)
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 {
// 解決點擊取消選擇商品時,
// 店鋪全選按鈕取消選擇狀態(tài),不會不變成全不選
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) {
//保存商品點擊狀態(tài)
list.get(parentposition).getList().get(chaildposition).setIscheck(isSelected);
//通知店鋪選擇的狀態(tài)
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);
}
//計算店鋪的選擇數(shù)量
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;
}
//計算每個店鋪商品的選擇數(shù)量
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的點擊變化事件進行回調(diào)
if (mCallBack!=null){
mCallBack.OnCheckListener(isChecked,position);
}
}
});
//實現(xiàn)第二層RecyclerView的回調(diào)接口
holder.adapter.setCallBack(new recyclerAdapter1.allCheck() {
@Override
public void OnCheckListener(boolean isChecked, int childpostion) {
//將店鋪商品的checkbox的點擊變化事件進行回調(diào)
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{
//回調(diào)函數(shù) 將店鋪的checkbox的點擊變化事件進行回調(diào)
public void OnCheckListener(boolean isSelected,int position);
//回調(diào)函數(shù) 將店鋪商品的checkbox的點擊變化事件進行回調(diào)
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的點擊變化事件進行回調(diào)給第一個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 {
//回調(diào)函數(shù) 將店鋪商品的checkbox的點擊變化事件進行回調(diào)
public void OnCheckListener(boolean isChecked, int childpostion);
}
}
實體類保存數(shù)據(jù)和選擇狀態(tài):
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中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式
這篇文章主要介紹了Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式的相關(guān)資料,需要的朋友可以參考下2016-03-03
flutter中的網(wǎng)絡(luò)請求數(shù)據(jù)獲取詳解
這篇文章主要為大家介紹了flutter中的網(wǎng)絡(luò)請求數(shù)據(jù)獲取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
詳解Android ContentProvider的基本原理和使用
ContentProvider(內(nèi)容提供者)是 Android 的四大組件之一,管理 Android 以結(jié)構(gòu)化方式存放的數(shù)據(jù),以相對安全的方式封裝數(shù)據(jù)(表)并且提供簡易的處理機制和統(tǒng)一的訪問接口供其他程序調(diào)用2021-06-06
Android使用DrawerLayout實現(xiàn)雙向側(cè)滑菜單
這篇文章主要為大家詳細介紹了Android使用DrawerLayout實現(xiàn)雙向側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
android RecyclerView添加footerview詳解
大家好,本篇文章主要講的是android RecyclerView添加footerview詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

