android中常常要用到ListView,有时也要用到ExpandableListView,如在手机设置中,对于分类有很好的效果,会用ListView的人一定会用ExpandableListView,因为ExpandableListView extends ListView的,下面来看个简单的例子
运行效果图:
导入依赖
compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.okhttp3:okhttp:3.9.0'
记得要把okhttp的原生文件夹复制进去,话不多说请看代码:
MainActivity布局:
适配器的Group布局:
适配器child布局:
接下来看代码:
用MVP获取的数据首先先在项目里创建三个文件夹分别是:model、view、persenter
model页面代码传的参数:
public interface GoWuCheModel {
public void get(String uid, Callback callback);
}
model页面的代码:
public class MyGoWuCheModel implements GoWuCheModel {
@Override
public void get(String uid, Callback callback) {
HashMap map = new HashMap<>();
map.put("uid", uid);
OkHttp3Utils.doPost("http://120.27.23.105/product/getCarts?source=android", map, callback);
}
}
view页面的代码:
public interface GoWuCheView {
public void success(ShopBean bean);
public void failuer(Exception e);
}
persenter页面的代码:
public class GoWuChePersenter {
GoWuCheView view;
private final MyGoWuCheModel model;
public GoWuChePersenter(GoWuCheView view) {
this.view = view;
model = new MyGoWuCheModel();
}
public void getData(String uid) {
model.get(uid, new onUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
if (view != null) {
view.failuer(e);
}
}
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
ShopBean bean = gson.fromJson(result, ShopBean.class);
view.success(bean);
}
});
}
public void saaa() {
this.view = null;
}
}
MainActivity页面代码:
public class MainActivity extends AppCompatActivity implements GoWuCheView {
private GoWuChePersenter persenter;
private ExpandableListView mElv;
private CheckBox mCb;
private TextView mTvTotal;
private TextView mTvCount;
private ElvAdapter elvAdapter;
List group = new ArrayList<>();
List> child = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
persenter = new GoWuChePersenter(this);
persenter.getData("99");
mCb.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
elvAdapter.AllOrNone(mCb.isChecked());
}
});
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mCb = (CheckBox) findViewById(R.id.cb);
mTvTotal = (TextView) findViewById(R.id.tvTotal);
mTvCount = (TextView) findViewById(R.id.tvCount);
}
public void setPriceAndCount(PriceAndCount priceAndCount) {
mTvTotal.setText("合计:" + priceAndCount.getPrice());
mTvCount.setText("去结算(" + priceAndCount.getCount() + ")");
}
public void setAllChecked(boolean bool) {
mCb.setChecked(bool);
}
@Override
public void success(ShopBean bean) {
for (int i = 0; i < bean.getData().size(); i++) {
group.add(bean.getData().get(i));
}
for (int i = 0; i < group.size(); i++) {
child.add(bean.getData().get(i).getList());
}
elvAdapter = new ElvAdapter(this, group, child);
mElv.setGroupIndicator(null);
mElv.setAdapter(elvAdapter);
for (int i = 0; i < group.size(); i++) {
mElv.expandGroup(i);
}
}
@Override
public void failuer(Exception e) {
}
@Override
protected void onDestroy() {
super.onDestroy();
persenter.saaa();
}
}
adapter页面代码:
public class ElvAdapter extends baseExpandableListAdapter {
private final LayoutInflater inflater;
private Context context;
private List group;
private List> child;
public ElvAdapter(Context context, List group, List> child) {
this.context = context;
this.group = group;
this.child = child;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(groupPosition).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 false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view;
Log.i("============", "getGroupView: ");
final GroupViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.elv_group, null);
holder = new GroupViewHolder();
holder.tv = view.findViewById(R.id.tv_group);
holder.cbGroup = view.findViewById(R.id.cb_group);
view.setTag(holder);
} else {
view = convertView;
holder = (GroupViewHolder) view.getTag();
}
final ShopBean.Data data = group.get(groupPosition);
holder.tv.setText(data.getSellerName());
holder.cbGroup.setChecked(data.isCheck());
holder.cbGroup.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
//需要改变三个CheckBox的状态值
//1.一级列表的CheckBox的状态值
data.setCheck(holder.cbGroup.isChecked());
//二级列表的CheckBox的状态值
setChildrenCb(groupPosition, holder.cbGroup.isChecked());
//全选的CheckBox状态值
((MainActivity) context).setAllChecked(isAllGroupCbChecked());
//计算钱和数量并显示
setPriceAndCount();
//刷新界面
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view1;
final ChildViewHolder holder1;
if (convertView == null) {
view1 = inflater.inflate(R.layout.elv_child, null);
holder1 = new ChildViewHolder();
holder1.iv = view1.findViewById(R.id.iv);
holder1.tvTitle = view1.findViewById(R.id.tv_title);
holder1.tvSubhead = view1.findViewById(R.id.tvSubhead);
holder1.tvPrice = view1.findViewById(R.id.tvPrice);
holder1.cbChild = view1.findViewById(R.id.cb_child);
holder1.btDel = view1.findViewById(R.id.btDel);
holder1.tvNum = view1.findViewById(R.id.tvNum);
holder1.ivDel = view1.findViewById(R.id.ivDel);
holder1.ivAdd = view1.findViewById(R.id.ivAdd);
view1.setTag(holder1);
} else {
view1 = convertView;
holder1 = (ChildViewHolder) view1.getTag();
}
final ShopBean.Data.List listBean = child.get(groupPosition).get(childPosition);
String images = listBean.getImages();
Glide.with(context).load(images.split("\|")[0]).into(holder1.iv);
holder1.tvTitle.setText(listBean.getTitle());
holder1.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked());
holder1.tvSubhead.setText(listBean.getSubhead());
holder1.tvPrice.setText(listBean.getPrice() + "元");
holder1.tvNum.setText(listBean.getCount() + "");
holder1.cbChild.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
//需要改变三个CheckBox的状态值
//2.二级列表的checkbox状态值
listBean.setChecked(holder1.cbChild.isChecked());
//1.一级列表的CheckBox的状态值
group.get(groupPosition).setCheck(isAllChildCbChecked(groupPosition));
//3.全选的CheckBox状态值
((MainActivity) context).setAllChecked(isAllGroupCbChecked());
//计算钱和数量并显示
setPriceAndCount();
//刷新界面
notifyDataSetChanged();
}
});
//加一件商品
holder1.ivAdd.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
int count = listBean.getCount();
count++;
//改变JavaBean里的状态值
listBean.setCount(count);
//计算钱和数量并显示
setPriceAndCount();
//刷新列表
notifyDataSetChanged();
}
});
//减少一件商品
holder1.ivDel.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
int count = listBean.getCount();
if (count <= 1) {
count = 1;
} else {
count--;
}
//改变JavaBean里的状态值
listBean.setCount(count);
//计算钱和数量并显示
setPriceAndCount();
//刷新列表
notifyDataSetChanged();
}
});
//删除
holder1.btDel.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
//其时就是删除集合
List listBeans = child.get(groupPosition);
if (listBeans.size() > 0) {
listBeans.remove(childPosition);
}
if (listBeans.size() == 0) {
child.remove(groupPosition);
group.remove(groupPosition);
}
//计算钱和数量并显示
setPriceAndCount();
//改变全选状态
((MainActivity) context).setAllChecked(isAllGroupCbChecked());
//刷新列表
notifyDataSetChanged();
}
});
return view1;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private void setChildrenCb(int groupPosition, boolean bool) {
List listBeans = child.get(groupPosition);
for (int i = 0; i < listBeans.size(); i++) {
listBeans.get(i).setChecked(bool);
}
}
private boolean isAllGroupCbChecked() {
if (group.size() == 0) {
return false;
}
for (int i = 0; i < group.size(); i++) {
if (!group.get(i).isCheck()) {
return false;
}
}
return true;
}
private boolean isAllChildCbChecked(int groupPosition) {
List listBeans = child.get(groupPosition);
for (int i = 0; i < listBeans.size(); i++) {
if (!listBeans.get(i).isChecked()) {
return false;
}
}
return true;
}
private void setPriceAndCount(){
((MainActivity)context).setPriceAndCount(compute());
}
private PriceAndCount compute(){
double price = 0;
int count = 0;
for (int i = 0; i < group.size(); i++) {
List listBeans = child.get(i);
for (int j = 0; j
bean类代码:
创建一个PriceAndCount 代码
public class PriceAndCount {
private double price;
private int count;
public PriceAndCount(double price, int count) {
this.price = price;
this.count = count;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
bean类:
public class ShopBean {
private String msg;
private String code;
private List data;
public void setMsg(String msg) {
this.msg = msg;
}
public void setCode(String code) {
this.code = code;
}
public void setData(List data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public String getCode() {
return code;
}
public List getData() {
return data;
}
public static class Data {
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
private boolean isCheck;
private String sellerName;
private String sellerid;
private java.util.List list;
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public void setList(java.util.List list) {
this.list = list;
}
public String getSellerName() {
return sellerName;
}
public String getSellerid() {
return sellerid;
}
public java.util.List getList() {
return list;
}
public static class List {
private boolean isChecked;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public void setImages(String images) {
this.images = images;
}
public void setNum(int num) {
this.num = num;
}
public void setPid(int pid) {
this.pid = pid;
}
public void setPrice(double price) {
this.price = price;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public void setSelected(int selected) {
this.selected = selected;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public void setTitle(String title) {
this.title = title;
}
public double getBargainPrice() {
return bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public String getImages() {
return images;
}
public int getNum() {
return num;
}
public int getPid() {
return pid;
}
public double getPrice() {
return price;
}
public int getPscid() {
return pscid;
}
public int getSelected() {
return selected;
}
public int getSellerid() {
return sellerid;
}
public String getSubhead() {
return subhead;
}
public String getTitle() {
return title;
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



