Android实现二级列表展示

Android实现二级列表展示_第1张图片

1:显示布局文件

 

2:activity中主要代码

//获取适配器
 CourseMenuAdapter courseMenuAdapter = new CourseMenuAdapter(getActivity(),data.getData());
                expandablelistview.setAdapter(courseMenuAdapter);

3:适配器中

/**
 * date:2020/6/28
 * author: wsm(Administrator)
 * function:
 */
public class CourseMenuAdapter extends BaseExpandableListAdapter {
    private Activity activity;
    private List list;


    public CourseMenuAdapter(Activity activity, List list) {
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return list.get(i).getMenu() == null ? 0 : list.get(i).getMenu().size();
    }

    //名称
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder1 holder1;
        if (view == null) {
            view = View.inflate(viewGroup.getContext(), R.layout.menu_itemone, null);
            holder1 = new ViewHolder1(view);
            view.setTag(holder1);
        } else {
            holder1 = (ViewHolder1) view.getTag();
        }
        holder1.mOnename.setText(list.get(i).getClassX());
        return view;
    }

    //内容
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewHolder2 holder2;
        if (view == null) {
            view = View.inflate(viewGroup.getContext(), R.layout.menu_itemtwo, null);
            holder2 = new ViewHolder2(view);
            view.setTag(holder2);
        } else {
            holder2 = (ViewHolder2) view.getTag();
        }
        holder2.mTwoname.setText(list.get(i).getMenu().get(i).getName());
        return view;
    }


    //组的数据
    public static class ViewHolder1 {


        private final TextView mOnename;

        public ViewHolder1(View itemview) {
            mOnename = itemview.findViewById(R.id.onename);

        }

    }

    //组的数据
    public static class ViewHolder2 {


        private final TextView mTwoname;

        public ViewHolder2(View itemview) {
            mTwoname = itemview.findViewById(R.id.twoname);
        }

    }


//------------------------------------以下方法无用

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

4:网络请求bean类就不展示了,按照接口直接请求即可
5:适配器中两个布局文件按照自己设计图完成就可以
哪里没看懂的小伙伴们可以私信我

你可能感兴趣的:(Android实现二级列表展示)