您的位置:首页 > 理论基础 > 计算机网络

二级列表ExpandableListView+gridview网络请求数据模拟商城展示

2017-09-07 09:40 411 查看
模拟商城界面,二级列表(
ExpandableListView
)是分类,里面的item嵌套gridview展示商品
//这个是放入了一个listview的点击事件中,所以传入了不同的点击位置,如果不需要和listview联动的话就不需要了。
private void liebiao1(final int post) {
exlist1 = new ArrayList<>();
//拼接网络接口
String ss = CLASSIFY0 + arr.get(post).getGc_id();
Log.e("ss------", ss);
//封装的okhttp数据请求,也可以按照自己的用其他的方式
myhttp.get(getActivity(), ss, Expand1.class, new MyInterface() {
@Override
public void onSuccess(final Basebean basebean) {
Expand1 bean = (Expand1) basebean;
//定义二级列表的存储,和二维数组一样,里面的泛型是解析bean类
final ArrayList<List<Expand2.DatasBean.ClassListBean>> map = new ArrayList<>();
map.clear();
//一级列表添加数据
exlist1.addAll(bean.getDatas().getClass_list());
Log.e("----------------", exlist1.toString());
//通过循环拿到二级列表
for (int i = 0; i < exlist1.size(); i++) {
final int index=i;
//子类的接口拼接
final String url = Url.CLASSIFY0 + exlist1.get(i).getGc_id();
Log.e("----",url);
myhttp.get(getActivity(),url, Expand2.class, new MyInterface() {
@Override
public void onSuccess(final Basebean basebean) {

Expand2 expand2= (Expand2) basebean;
List<Expand2.DatasBean.ClassListBean> list3 = expand2.getDatas().getClass_list();

//二级列表添加数据
map.add(list3);
//异步加载判断是否加载完成,完成了就进入适配器
if (index == exlist1.size() - 1) {
expanAdapter = new ExpandaListViewAdapter( exlist1, map,getActivity());
expand.setAdapter(expanAdapter);
int count = expand.getCount();
for (int j=0;j<count;j++){
expand.expandGroup(j);
}
}
}
});

}
}
});
}
--------------------------------------------------------------------
二级列表适配器:
public class ExpandaListViewAdapter extends BaseExpandableListAdapter {private List<Expand1.DatasBean.ClassListBean> group;private ArrayList<List<Expand2.DatasBean.ClassListBean>> child;private Context context;public ExpandaListViewAdapter(List<Expand1.DatasBean.ClassListBean> group, ArrayList<List<Expand2.DatasBean.ClassListBean>> child, Context context) {this.group = group;this.child = child;this.context = context;}@Overridepublic int getGroupCount() {return group.size();}@Overridepublic int getChildrenCount(int i) {cd75return 1;}@Overridepublic Expand1.DatasBean.ClassListBean getGroup(int i) {return group.get(i);}@Overridepublic Expand2.DatasBean.ClassListBean getChild(int i, int i1) {return child.get(i).get(i1);}@Overridepublic long getGroupId(int i) {return i;}@Overridepublic long getChildId(int i, int i1) {return i1;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {View v = View.inflate(context, R.layout.expand_listview, null);TextView textView = v.findViewById(R.id.expan_text);textView.setText(group.get(i).getGc_name());//        v.setClickable(true);return v;}@Overridepublic View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {View v = View.inflate(context, R.layout.expand_item, null);GridView gridView = v.findViewById(R.id.expan2_gridview);List<Expand2.DatasBean.ClassListBean> classListBeen = child.get(i);//        child.get(i).get(i1);gridView.setAdapter(new MyGridAdapter(context, classListBeen));gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Toast.makeText(context, "二级列表", Toast.LENGTH_SHORT).show();}});return v;}@Overridepublic boolean isChildSelectable(int i, int i1) {return true;}}
------------------------------------------------------------
注意的是,二级列表里嵌套的gridview如果直接加载会有一个高度问题,里面的数据无法全部展示,这时需要我们自定义一个类继承gridview,重写高度计算,布局中的gridview就是用我们自定义好的类
public class ShowAllShopsType_list_grid extends GridView {public ShowAllShopsType_list_grid(Context context, AttributeSet attrs) {super(context, attrs);}/*** 设置不滚动*/public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}
--------------------------------------------------------------
gridview适配器
public class GraidAdapter extends BaseAdapter {private Context context;private List<String> arr;public GraidAdapter(Context context, List<String> shuju) {this.context = context;this.arr = shuju;}@Overridepublic int getCount() {return arr==null?0:arr.size();}@Overridepublic Object getItem(int i) {return arr.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHodle hodle;if (view==null){view=View.inflate(context, R.layout.gridview_layout,null);hodle=new ViewHodle();hodle.text=view.findViewById(R.id.textview);view.setTag(hodle);}else {hodle= (ViewHodle) view.getTag();}hodle.text.setText(arr.get(i));return view;}class ViewHodle{TextView text;}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: