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

来源:互联网 发布:电脑硬盘坏了恢复数据 编辑:程序博客网 时间:2024/05/18 01:59
模拟商城界面,二级列表(
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;    }    @Override    public int getGroupCount() {        return group.size();    }    @Override    public int getChildrenCount(int i) {        return 1;    }    @Override    public Expand1.DatasBean.ClassListBean getGroup(int i) {        return group.get(i);    }    @Override    public Expand2.DatasBean.ClassListBean getChild(int i, int i1) {        return child.get(i).get(i1);    }    @Override    public long getGroupId(int i) {        return i;    }    @Override    public long getChildId(int i, int i1) {        return i1;    }    @Override    public boolean hasStableIds() {        return true;    }    @Override    public 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;    }    @Override    public 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() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                Toast.makeText(context, "二级列表", Toast.LENGTH_SHORT).show();            }        });        return v;    }    @Override    public 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;    }    @Override         public int getCount() {             return arr==null?0:arr.size();         }         @Override         public Object getItem(int i) {             return arr.get(i);         }         @Override         public long getItemId(int i) {             return i;         }         @Override         public 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;         }}

原创粉丝点击