android 仿淘寶京東購物車 ListView嵌套CheckBox

MyActivity===========================代碼以下

package com.example.ListViewBoxDemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import java.util.ArrayList;


/**
*
* 模仿淘寶購物車中的ListView 中嵌套CheckBox
* 實現每一個店鋪全選 已經不選中的功能
*實現原理 在bean中添加一個標記 經過CheckBox的改變標記來變化bean中的標記
* 具體不明白的加Q 513599210
*/
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
ArrayList<ArrayList<ShopBean>> arrayList = new ArrayList<>(); // 表明全部的加入購物車中的店鋪
ArrayList<ShopBean> arrayListone = new ArrayList<>(); //表明每一個店鋪
ArrayList<ShopBean> arrayListtwo = new ArrayList<>();
ArrayList<ShopBean> arrayListfow = new ArrayList<>();

ListView shopping_listview;
Context context;
CheckBox all_chekbox;
ItemAdapter itemAdapter;


int size; // 統計有多少個商品
int oksize; // 統計bean中的是否選中
boolean boxoutstate =true;// 表示CheckBox是外部的點擊仍是內部的方法 默認執行外部全選方法
boolean boxoutclickstate =true;// 是內部的item CheckBox 選中或者取消傳遞出來的參數接收

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.context = this;
shopping_listview = (ListView) findViewById(R.id.shopping_listview);
all_chekbox = (CheckBox) findViewById(R.id.all_chekbox);
arrayListone.add(new ShopBean());
arrayListone.add(new ShopBean());
arrayListone.add(new ShopBean());
arrayListtwo.add(new ShopBean());
arrayListtwo.add(new ShopBean());
arrayListtwo.add(new ShopBean());
arrayListfow.add(new ShopBean());
arrayListfow.add(new ShopBean());
arrayListfow.add(new ShopBean());
arrayList.add(arrayListone);
arrayList.add(arrayListtwo);
arrayList.add(arrayListfow);

itemAdapter = new ItemAdapter(this, arrayList, new ItemAdapter.Iparentbox() {
@Override
public void clickbox(boolean boxstate) {
// 選中店鋪中的CheckBox回調
boxoutclickstate=boxstate;
// all_chekbox.setChecked(boxstate);

size = 0;
oksize = 0;
for (ArrayList<ShopBean> arrbean : arrayList) {
for (ShopBean bean : arrbean) {
size++; // 統計有 多少個元素
}
}
for (ArrayList<ShopBean> arrbean : arrayList) {
for (ShopBean bean : arrbean) {
if (bean.parentbox) {
oksize++;// 統計元素中有多少個是已經選中
}

}
}
System.out.println("====saze===" + size + "===oksaze====" + oksize);
// 如何bean中的表示選中 與 arrayList相等設置外部box全選
if (size == oksize) {
all_chekbox.setChecked(true);
} else {
// 不成立設置box不是全選 同時改變 boxoutstate 標記 爲外內部box執行方法
boxoutstate = false;
all_chekbox.setChecked(false);
}


}
}, R.layout.item_shop);

nextAdate();


all_chekbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("boxoutstate==="+boxoutstate);
if (isChecked) {
for (ArrayList<ShopBean> arrbean : arrayList) {
for (ShopBean bean : arrbean) {
bean.parentbox = isChecked;
}
}
itemAdapter.setBoxState(isChecked);
} else {
// 這裏是執行了內部itembox 的執行
if((boxoutstate==false)&&(boxoutclickstate==false)){
System.out.println("內部的box點擊");
}else {
for (ArrayList<ShopBean> arrbean : arrayList) {
for (ShopBean bean : arrbean) {
bean.parentbox = isChecked;
}
}
itemAdapter.setBoxState(isChecked);
}


}


}

});

}

// 更新數據
private void nextAdate() {
shopping_listview.setAdapter(itemAdapter);
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
item的代碼
===========================================================================================================================================
package com.example.ListViewBoxDemo;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
* Created by ADMIN on 2015/12/16.
*/
public class ItemAdapter extends CommonAdapter<ArrayList<ShopBean>> {
Context contextData;
List<ArrayList<ShopBean>> datas;
Iparentbox iparentbox; // 內部box 的選中 取消接口回調

public ItemAdapter(Context context, List<ArrayList<ShopBean>> datas, Iparentbox iparentbox, int layoutId) {
super(context, datas, layoutId);
this.contextData = context;
this.datas = datas;
this.iparentbox = iparentbox;
}

public interface Iparentbox {
public void clickbox(boolean boxstate);
}

@Override
public void convert(ViewHolder holder, ArrayList<ShopBean> shopBeans) {
CheckBox determine_chekbox = holder.getView(R.id.determine_chekbox);
// 這是判斷是否
int h = 0;
boolean statechildbox = false;
for (ShopBean shoppingCartBean : datas.get(holder.getPosition())) {
if (shoppingCartBean.parentbox) {
h++;
}
}
System.out.println("====h===" + h);
// 判斷加入購物車中的每一個店鋪的多個商品是否全選 全選 店鋪的box 也選中
if (datas.get(holder.getPosition()).size() == h) {
determine_chekbox.setChecked(true);
System.out.println("執行過來了");
} else if (h == 0) {
System.out.println("執行過來了===");
determine_chekbox.setChecked(false);
} else {

}

// 店鋪 Box選中或者未選中時候刷新 裏面的商品
determine_chekbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ArrayList<ShopBean> shoppingCartBeans1 = datas.get(holder.getPosition());
if (isChecked) {
for (ShopBean databean : shoppingCartBeans1) {
databean.parentbox = isChecked;
}
notifyDataSetChanged();
iparentbox.clickbox(isChecked);
} else {
for (ShopBean databean : shoppingCartBeans1) {
databean.parentbox = isChecked;
}
notifyDataSetChanged();
iparentbox.clickbox(isChecked);
}

}
});


LinearLayout add_shoping_item = holder.getView(R.id.add_shoping_item);
add_shoping_item.removeAllViews();
for (int i = 0; i < shopBeans.size(); i++) {

View inflate = View.inflate(contextData, R.layout.add_item_shoppingcart, null);
CheckBox checkBox = (CheckBox) inflate.findViewById(R.id.add_chekbox);
checkBox.setChecked(shopBeans.get(i).parentbox);
add_shoping_item.addView(inflate);

final int finalI = i;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
shopBeans.get(finalI).parentbox = isChecked;
System.out.println("子控件選中執行過來");
notifyDataSetChanged();
iparentbox.clickbox(isChecked);

} else {
shopBeans.get(finalI).parentbox = isChecked;
iparentbox.clickbox(isChecked);
notifyDataSetChanged();
}

}
});

}
}

public void setBoxState(boolean state) {
System.out.println("state===" + state);
if (state) {
notifyDataSetChanged();
} else {
notifyDataSetChanged();
}
}


}

===============================================================


源碼 http://download.csdn.net/detail/qq_15696769/9364407
相關文章
相關標籤/搜索