listView的二级联动

布局

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.llxy.sanzu.yiku.fragment.found_chayao_fragment.Xiyao_Fragment">
            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView_xiyao"
        />

下面是适配器传输的数据

MyXiYaoAdapter myXiYaoAdapter = new MyXiYaoAdapter(getContext(),data0);
adapter

public class MyXiYaoAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List data0;
    private LayoutInflater layoutInflater;
    public MyXiYaoAdapter(Context context, List data0) {
        this.context = context;
        this.data0 = data0;
        layoutInflater = LayoutInflater.from(context);
    }

    //父级组的数量
    @Override
    public int getGroupCount() {
        return data0.size();
    }

    //子级组的数量
    @Override
    public int getChildrenCount(int i) {
        List submenu0 = data0.get(i).getSubmenu0();
        Log.d("zdl","getChildrenCount"+submenu0.size());
        return submenu0.size();
    }

    //获取与给定的组相关的数据,得到数组父级中元素的数据
    @Override
    public Object getGroup(int i) {
        return data0.get(i);
    }
    //获取与孩子在给定的组相关的数据,得到数组arms中元素的数据
    @Override
    public Object getChild(int i, int i1) {
        return data0.get(i).getSubmenu0().get(i1);
    }
    //获取组在给定的位置编号,即armTypes中元素的ID
    @Override
    public long getGroupId(int i) {
        return i;
    }
    //获取在给定的组的儿童的ID,就是子级中元素的ID
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
    //取一个视图显示给定组,存放父级
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            view = layoutInflater.inflate(R.layout.item_medicine,null);
        TextView tv = view.findViewById(R.id.textView_medicine);

        Western.DataBean.Data0Bean data0Bean = data0.get(i);
        Log.d("zdl","dataOBean"+data0Bean);
        tv.setText(data0Bean.getName());
        return view;
    }

    获取一个视图显示给定组  子级
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        view = layoutInflater.inflate(R.layout.item_medicine,null);
        TextView tv = view.findViewById(R.id.textView_medicine);

        Western.DataBean.Data0Bean.Submenu0Bean submenu0Bean = data0.get(i).getSubmenu0().get(i1);
        tv.setText(submenu0Bean.getName());
        return view;
    }
    //孩子在指定的位置是可选的 子级元素可点
   @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    //表示孩子是否和组ID是跨基础数据的更改稳定
    @Override
    public boolean hasStableIds() {
        return false;
    }
    class MyXiYaoAdapterView{
        TextView textView_medicine;
    }
}


你可能感兴趣的:(listView的二级联动)