效果图如下:
数据是请求接口得到的,需要导入一些依赖
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.google.code.gson:gson:2.8.2'
清单文件中别忘了添加请求网络的权限
主布局activity_lei_expandable.xml
主Activity:LeiExpandableActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.Toast;
import com.littlejie.app.ImageClick.AbstractUiCallBack;
import com.littlejie.app.ImageClick.OkhttpUtils;
import com.littlejie.app.R;
import java.util.HashMap;
import java.util.Map;
public class LeiExpandableActivity extends AppCompatActivity {
private ExpandableListView expandablelistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lei_expandable);
initView();
getGoods();
}
private void getGoods() {
//http://120.27.23.105/product/getCarts?uid=100
String url = "http://120.27.23.105/product/getCarts";
Map map = new HashMap<>();
map.put("uid","100");
OkhttpUtils.getInstance().asy(map, url, new AbstractUiCallBack() {
@Override
public void success(GetCartsBean getCartsBean) {
expandablelistview.setAdapter(new LeiExpandableAdapter(LeiExpandableActivity.this,getCartsBean.getData()));
}
@Override
public void failure(Exception e) {
Toast.makeText(LeiExpandableActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
}
});
}
private void initView(){
expandablelistview = (ExpandableListView) findViewById(R.id.lei_expandablelistview);
//去掉自带箭头
// expandablelistview.setGroupIndicator(null);
}
}
适配器:LeiExpandableAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.littlejie.app.R;
import java.util.List;
/**
* Created by PC-dev on 2018/11/26.
* expandablelistview继承自 BaseExpandableListAdapter
*/
public class LeiExpandableAdapter extends BaseExpandableListAdapter {
Context context;
List data;
public LeiExpandableAdapter(Context context, List data) {
this.context = context;
this.data = data;//整体数据
}
/*
* 一级列表个数
* */
@Override
public int getGroupCount() {
return data.size();
}
/*
* 每个二级列表的个数
* */
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).getList().size();
}
/*
* 一级列表中的每个item
* */
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
/*
* 二级列表中的每个item
* */
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.get(groupPosition).getList().get(childPosition);
}
/*
* 组的id
* */
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/*
* 子选项的id
* */
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/*
* 每个item的id是固定的,一般为true
* */
@Override
public boolean hasStableIds() {
return true;
}
/*
* isExpanded是否展开
* */
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = View.inflate(context,R.layout.expandable_groups,null);
}
TextView groups_text = (TextView) convertView.findViewById(R.id.groups_text);
groups_text.setText(data.get(groupPosition).getSellerName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = View.inflate(context,R.layout.expandable_childs,null);
}
TextView childs_text = (TextView) convertView.findViewById(R.id.childs_text);
childs_text.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
return convertView;
}
/*
* 二级列表中每个选项能否被选中,如果有点击事件一定有设为true
* */
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
请求接口用的2个封装类
OkhttpUtils
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class OkhttpUtils {
private static OkhttpUtils okhttpUtils = null ;
private OkhttpUtils(){
}
public static OkhttpUtils getInstance(){
if(okhttpUtils == null){
okhttpUtils = new OkhttpUtils();
client = new OkHttpClient.Builder()
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.build();
}
return okhttpUtils ;
}
private static OkHttpClient client ;
public void asy(Map params, String url, AbstractUiCallBack callBack){
Request request = null ;
if(params != null){ //如果参数不为空,使用post
FormBody.Builder builder = new FormBody.Builder() ;
for(Map.Entry entry : params.entrySet()){
builder.add(entry.getKey(),entry.getValue());
}
FormBody body = builder.build();
request = new Request.Builder() //post
.url(url)
.post(body)
.build();
} else { //如果参数为空 ,使用get
request = new Request.Builder()
.url(url)
.build();
}
client.newCall(request).enqueue(callBack);
}
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
public static void postFile(Map map, String url, File file, AbstractUiCallBack callBack){
String[] array = file.getAbsolutePath().split("\\/");
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
for(Map.Entry entry : map.entrySet()){
builder.addFormDataPart(entry.getKey(),entry.getValue());
}
builder.addFormDataPart("imageFileName",array[array.length-1]);
if(file.exists() && file.length() > 0){
builder.addFormDataPart("image",array[array.length-1], RequestBody.create(MEDIA_TYPE_PNG,file));
}
MultipartBody body = builder.build() ;
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(callBack);
}
}
AbstractUiCallBack
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public abstract class AbstractUiCallBack implements Callback {
/**
* 成功回调
* @param t
*/
public abstract void success(T t);
/**
* 失败回调
* @param e
*/
public abstract void failure(Exception e);
private Handler handler = null ;
private Class clazz ;
public AbstractUiCallBack(){
handler = new Handler(Looper.getMainLooper());
// 得到的是一个 AbstractUiCallBack 的Type
Type type = getClass().getGenericSuperclass() ;
// 得到的是T的实际Type
Type[] arr = ((ParameterizedType)type).getActualTypeArguments() ;
clazz = (Class) arr[0] ;
}
@Override
public void onFailure(Call call, IOException e) {
failure(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String result = response.body().string();
System.out.println("result = " + result);
Gson gson = new Gson();
final T t = (T) gson.fromJson(result,clazz);
handler.post(new Runnable() {
@Override
public void run() {
success(t);
}
});
} catch (IOException e) {
e.printStackTrace();
failure(e);
} catch (JsonSyntaxException e) {
e.printStackTrace();
failure(e);
}
}
}
接口返回数据的模型GetCartsBean
import java.util.List;
/**
* Created by PC-dev on 2018/12/27.
* 获取购物车bean实体类
*/
public class GetCartsBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":8,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":10,"pid":26,"price":88,"pscid":2,"selected":0,"sellerid":3,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","num":1,"pid":59,"price":5599,"pscid":40,"selected":0,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":7,"pid":60,"price":13888,"pscid":40,"selected":0,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":28,"price":599,"pscid":2,"selected":0,"sellerid":5,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":29,"price":588,"pscid":2,"selected":0,"sellerid":6,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":62,"price":15999,"pscid":40,"selected":0,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"}]
*/
private String msg;
private String code;
/**
* list : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":8,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}]
* sellerName : 商家1
* sellerid : 1
*/
private List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
private String sellerName;
private String sellerid;
/**
* bargainPrice : 11800.0
* createtime : 2017-10-10T17:33:37
* detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg
* num : 8
* pid : 57
* price : 5199.0
* pscid : 40
* selected : 0
* sellerid : 1
* subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统
* title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银
*/
private List list;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
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 double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
适配器中引入了2个布局,分别是一级列表和二级列表的布局文件
一级列表布局文件 expandable_groups.xml
二级列表布局文件expandable_childs.xml