1.bean类
2.
public class FragmentTwo extends Fragment {
private ShoppingCartBean shoppingCartBean;
private ExpandableListView expandableListView;
private ShoppingCartAdapter shoppingCartAdapter;
private CheckBox checkAll;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag2, container, false);
expandableListView = view.findViewById(R.id.expandable);
checkAll = view.findViewById(R.id.select_all);
initTestData();
initView();
return view;
}
private void initView() {
shoppingCartAdapter = new ShoppingCartAdapter(getActivity());
expandableListView.setAdapter(shoppingCartAdapter);
shoppingCartAdapter.setData(shoppingCartBean);
for (int i = 0; i < shoppingCartBean.getData().size(); i++) {
expandableListView.expandGroup(i);
}
shoppingCartAdapter.setCheckBox(checkAll);
}
private void initTestData() {
shoppingCartBean = new ShoppingCartBean();
ArrayList all = new ArrayList();
ArrayList arrayListA = new ArrayList();
ArrayList arrayListB = new ArrayList();
ShoppingCartBean.DataBean.ListBean goods1 = new ShoppingCartBean.DataBean.ListBean();
goods1.setTitle("商品1");
goods1.setPrice("10");
goods1.setNum("1");
goods1.setCheck(false);
ShoppingCartBean.DataBean.ListBean goods2 = new ShoppingCartBean.DataBean.ListBean();
goods2.setTitle("商品2");
goods2.setPrice("20");
goods2.setNum("1");
goods2.setCheck(false);
ShoppingCartBean.DataBean.ListBean goods3 = new ShoppingCartBean.DataBean.ListBean();
goods3.setTitle("商品3");
goods3.setPrice("30");
goods3.setNum("1");
goods3.setCheck(false);
ShoppingCartBean.DataBean.ListBean goods4 = new ShoppingCartBean.DataBean.ListBean();
goods4.setTitle("商品4");
goods4.setPrice("40");
goods4.setNum("1");
goods4.setCheck(false);
arrayListA.add(goods1);
arrayListA.add(goods2);
arrayListB.add(goods3);
arrayListB.add(goods4);
ShoppingCartBean.DataBean dataBean1 = new ShoppingCartBean.DataBean();
dataBean1.setList(arrayListA);
dataBean1.setSellerName("天猫");
ShoppingCartBean.DataBean dataBean2 = new ShoppingCartBean.DataBean();
dataBean2.setList(arrayListB);
dataBean2.setSellerName("京东");
all.add(dataBean1);
all.add(dataBean2);
shoppingCartBean.setData(all);
}
}
frag2.xml
3.
public class ShoppingCartAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ShoppingCartBean mShoppingCartBean;
public ShoppingCartAdapter(Context context){
this.mContext = context;
}
public void setData(ShoppingCartBean shoppingCartBean){
this.mShoppingCartBean = shoppingCartBean;
}
@Override
public int getGroupCount() {
if(mShoppingCartBean == null){
return 0;
}
return mShoppingCartBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
if(mShoppingCartBean == null){
return 0;
}
return mShoppingCartBean.getData().get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ParentViewHolder parentViewHolder;
if(convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.groupitem, null);
parentViewHolder = new ParentViewHolder();
parentViewHolder.shopName = convertView.findViewById(R.id.shopName);
parentViewHolder.checkBox = convertView.findViewById(R.id.checkbox_parent);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.checkBox.setChecked(mShoppingCartBean.getData().get(groupPosition).isCheck());
parentViewHolder.shopName.setText(mShoppingCartBean.getData().get(groupPosition).getSellerName());
parentViewHolder.checkBox.setTag(groupPosition);
parentViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
boolean checked = checkBox.isChecked();
int groupPosition = Integer.parseInt(checkBox.getTag().toString());
mShoppingCartBean.getData().get(groupPosition).setCheck(checked);
selectGroup(groupPosition,checked);
boolean selectAllGroup = isSelectAllGroup();
mCheckBox.setChecked(selectAllGroup);
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if(convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.childitem, null);
childViewHolder = new ChildViewHolder();
childViewHolder.goodsName = convertView.findViewById(R.id.goodsName);
childViewHolder.checkBox = convertView.findViewById(R.id.checkbox_child);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.checkBox.setChecked(mShoppingCartBean.getData().get(groupPosition).getList().get(childPosition).isCheck());
childViewHolder.goodsName.setText(mShoppingCartBean.getData().get(groupPosition).getList().get(childPosition).getTitle());
childViewHolder.checkBox.setTag(groupPosition+"#"+childPosition);
childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
String tag = (String) v.getTag();
int groupPosition = Integer.parseInt(tag.split("#")[0]);
int childPosition = Integer.parseInt(tag.split("#")[1]);
ShoppingCartBean.DataBean dataBean = mShoppingCartBean.getData().get(groupPosition);
ShoppingCartBean.DataBean.ListBean goodsBean = dataBean.getList().get(childPosition);
goodsBean.setCheck(checkBox.isChecked());
boolean selectGroup = isSelectGroup(groupPosition);
dataBean.setCheck(selectGroup);
boolean selectAllGroup = isSelectAllGroup();
mCheckBox.setChecked(selectAllGroup);
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
CheckBox mCheckBox;
public void setCheckBox(final CheckBox checkBox) {
this.mCheckBox = checkBox;
mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
selectAll(checkBox.isChecked());
}
});
}
public void selectGroup(int groupPosition,boolean isChecked){
for (int i = 0; i < mShoppingCartBean.getData().get(groupPosition).getList().size(); i++) {
ShoppingCartBean.DataBean.ListBean listBean = mShoppingCartBean.getData().get(groupPosition).getList().get(i);
listBean.setCheck(isChecked);
}
}
private void selectAll(boolean checked) {
for (int i = 0; i < mShoppingCartBean.getData().size(); i++) {
ShoppingCartBean.DataBean dataBean = mShoppingCartBean.getData().get(i);
dataBean.setCheck(checked);
for (int j = 0; j < dataBean.getList().size(); j++) {
ShoppingCartBean.DataBean.ListBean listBean = dataBean.getList().get(j);
listBean.setCheck(checked);
}
}
notifyDataSetChanged();
}
private boolean isSelectGroup(int groupPosition){
for (int i = 0; i < mShoppingCartBean.getData().get(groupPosition).getList().size(); i++) {
ShoppingCartBean.DataBean.ListBean listBean = mShoppingCartBean.getData().get(groupPosition).getList().get(i);
boolean check = listBean.isCheck();
if (!check){
return false;
}
}
return true;
}
private boolean isSelectAllGroup(){
for (int i = 0; i < mShoppingCartBean.getData().size(); i++) {
ShoppingCartBean.DataBean dataBean = mShoppingCartBean.getData().get(i);
boolean check = dataBean.isCheck();
if (!check) {
return false;
}
}
return true;
}
class ParentViewHolder {
TextView shopName;
CheckBox checkBox;
}
class ChildViewHolder {
TextView goodsName;
CheckBox checkBox;
}
}
4.布局
groupitem
//childitem