private List<BaseBean.DataBean> grouplists;
private Context context;
private TextView tv;
private Button jian;
private Button jia;
public MyExpandableAdapter(List<BaseBean.DataBean> grouplists, Context context) {
this.grouplists=grouplists;
this.context=context;
}
@Override
public int getGroupCount() {
return grouplists.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return grouplists.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return grouplists.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return grouplists.get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final Viewgroup viewgroup;
if (convertView == null) {
viewgroup=new Viewgroup();
convertView=View.inflate(context,R.layout.grouplist,null);
viewgroup.gck=convertView.findViewById(R.id.groupck);
viewgroup.gname=convertView.findViewById(R.id.grouptv);
convertView.setTag(viewgroup);
}else {
viewgroup= (Viewgroup) convertView.getTag();
}
viewgroup.gck.setChecked(grouplists.get(groupPosition).isGroupChecked());
viewgroup.gname.setText(grouplists.get(groupPosition).getSellerName());
EventBus.getDefault().post(countPriceAll());
viewgroup.gck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
grouplists.get(groupPosition).setGroupChecked(viewgroup.gck.isChecked());
for (int i = 0; i <grouplists.get(groupPosition).getList().size() ; i++) {
grouplists.get(groupPosition).getList().get(i).setChildChecked(viewgroup.gck.isChecked());
}
EventType eventType=new EventType();
eventType.setType(isAllChecked());
EventBus.getDefault().post(eventType);
EventBus.getDefault().post(countPriceAll());
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final Viewchild viewchild;
if (convertView == null) {
viewchild=new Viewchild();
convertView=View.inflate(context,R.layout.childlist,null);
viewchild.title=convertView.findViewById(R.id.child_title);
viewchild.price=convertView.findViewById(R.id.child_price);
viewchild.cck=convertView.findViewById(R.id.child_ck);
viewchild.cimg=convertView.findViewById(R.id.child_img);
viewchild.countView=convertView.findViewById(R.id.count);
convertView.setTag(viewchild);
}else {
viewchild= (Viewchild) convertView.getTag();
}
viewchild.cck.setChecked(grouplists.get(groupPosition).getList().get(childPosition).isChildChecked());
viewchild.price.setText("¥:"+grouplists.get(groupPosition).getList().get(childPosition).getPrice());
viewchild.title.setText(grouplists.get(groupPosition).getList().get(childPosition).getTitle());
tv = viewchild.countView.findViewById(R.id.tv);
jian = viewchild.countView.findViewById(R.id.left);
jia = viewchild.countView.findViewById(R.id.right);
tv.setText(grouplists.get(groupPosition).getList().get(childPosition).getNum()+"");
String images = grouplists.get(groupPosition).getList().get(childPosition).getImages();
String[] sp = images.split("\\|");
Glide.with(context).load(sp[0]).into(viewchild.cimg);
viewchild.cck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
grouplists.get(groupPosition).getList().get(childPosition).setChildChecked(viewchild.cck.isChecked());
grouplists.get(groupPosition).setGroupChecked(isGroupChecked(groupPosition));
EventType eventType=new EventType();
eventType.setType(isAllChecked());
EventBus.getDefault().post(eventType);
EventBus.getDefault().post(countPriceAll());
}
});
jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = grouplists.get(groupPosition).getList().get(childPosition).getNum();
if (i>1) {
i--;
grouplists.get(groupPosition).getList().get(childPosition).setNum(i);
}
EventType eventType=new EventType();
eventType.setType(isAllChecked());
EventBus.getDefault().post(eventType);
EventBus.getDefault().post(countPriceAll());
}
});
jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = grouplists.get(groupPosition).getList().get(childPosition).getNum();
i++;
grouplists.get(groupPosition).getList().get(childPosition).setNum(i);
EventType eventType=new EventType();
eventType.setType(isAllChecked());
EventBus.getDefault().post(eventType);
EventBus.getDefault().post(countPriceAll());
}
});
return convertView;
}
public boolean isGroupChecked(int groupPosition){
List<BaseBean.DataBean.ListBean> list = grouplists.get(groupPosition).getList();
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).isChildChecked()) {
return false;
}
}
return true;
}
public boolean isAllChecked(){
for (int i = 0; i < grouplists.size(); i++) {
if (!grouplists.get(i).isGroupChecked()) {
return false;
}
}
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class Viewgroup {
CheckBox gck;
TextView gname;
}
class Viewchild {
CheckBox cck;
TextView title,price;
CountView countView;
ImageView cimg;
}
public EventCount countPriceAll(){
int prices=0;
int counts=0;
for (int i = 0; i <grouplists.size() ; i++) {
for (int j = 0; j < grouplists.get(i).getList().size(); j++) {
if (grouplists.get(i).getList().get(j).isChildChecked()) {
prices+=grouplists.get(i).getList().get(j).getPrice()*grouplists.get(i).getList().get(j).getNum();
counts+=grouplists.get(i).getList().get(j).getNum();
}
}
}
EventCount eventCount = new EventCount();
eventCount.setCounts(counts);
eventCount.setPrices(prices);
return eventCount;
}
}
`
public class MainActivity extends AppCompatActivity implements ToBack<List<BaseBean.DataBean>> {
private static final String TAG = "MainActivity";
ExpandableListView exview;
List<BaseBean.DataBean> grouplists = new ArrayList<>();
List<BaseBean.DataBean> groupdel = new ArrayList<>();
List<BaseBean.DataBean.ListBean> childdel = new ArrayList<>();
@BindView(R.id.group_ck)
CheckBox groupCk;
@BindView(R.id.group_price)
TextView groupPrice;
@BindView(R.id.group_count)
TextView groupCount;
private MyExpandableAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exview = findViewById(R.id.exview);
ButterKnife.bind(this);
Model.getData(this);
EventBus.getDefault().register(this);
adapter = new MyExpandableAdapter(grouplists, this);
exview.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
}
@Subscribe
public void onEvent(EventCount eventCount) {
Log.e(TAG, "receive: "+ eventCount.getPrices());
groupCount.setText("加入购物车(" + eventCount.getCounts() + ")");
groupPrice.setText("合计:" + eventCount.getPrices() +".00");
// Log.e(TAG, "onEvent: " + groupPrice.getText().toString());
}
@Subscribe
public void onEventType(EventType eventType) {
groupCk.setChecked(eventType.isType());
adapter.notifyDataSetChanged();
}
@Override
public void success(List<BaseBean.DataBean> data) {
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data.get(i).getList().size(); j++) {
data.get(i).getList().get(j).setChildChecked(data.get(i).getList().get(j).getSelected() == 1 ? true : false);
}
data.get(i).setGroupChecked(isGroupChecked(data, i));
}
grouplists.addAll(data);
adapter.notifyDataSetChanged();
for (int i = 0; i < data.size(); i++) {
exview.expandGroup(i);
}
}
public boolean isGroupChecked(List<BaseBean.DataBean> data, int i) {
for (int j = 0; j < data.get(i).getList().size(); j++) {
if (!data.get(i).getList().get(j).isChildChecked()) {
return false;
}
}
return true;
}
@OnClick({R.id.group_ck, R.id.group_del})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.group_ck:
for (int i = 0; i < grouplists.size(); i++) {
grouplists.get(i).setGroupChecked(groupCk.isChecked());
for (int j = 0; j < grouplists.get(i).getList().size(); j++) {
grouplists.get(i).getList().get(j).setChildChecked(groupCk.isChecked());
}
}
adapter.notifyDataSetChanged();
break;
case R.id.group_del:
groupdel.clear();
childdel.clear();
for (int i = 0; i <grouplists.size() ; i++) {
if (grouplists.get(i).isGroupChecked()) {
groupdel.add(grouplists.get(i));
}else{
for (int j = 0; j <grouplists.get(i).getList().size() ; j++) {
if (grouplists.get(i).getList().get(j).isChildChecked()) {
BaseBean.DataBean.ListBean listBean = grouplists.get(i).getList().get(j);
childdel.add(listBean);
}
}
}
grouplists.get(i).getList().removeAll(childdel);
}
grouplists.removeAll(groupdel);
adapter.notifyDataSetChanged();
break;
}
}
}