ExpandableListView可扩展的下拉列表简单的使用


使用ExpandableListView不需要依赖任何框架直接使用即可。

首先,搭个简单的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.zzb.expandablelistview.MainActivity">

   <ExpandableListView
       android:id="@+id/edb_lv"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</LinearLayout>

接下来实例化,然后写个适配器,难点:主要是适配器比listview的复杂了点,慢慢看看还是很容易弄明白的。

public class MainActivity extends AppCompatActivity {
    private MyExpandableListViewAdapter adapter;
    private ExpandableListView listview;
    private String[] parentList = new String[]{"水果", "蔬菜", "肉类"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ExpandableListView) findViewById(R.id.edb_lv);
        adapter = new MyExpandableListViewAdapter(this,parentList);
        initialData();
        listview.setAdapter(adapter);
        listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //groupPositionchildPosition都是从0开始
                Toast.makeText(MainActivity.this, "点击了第"+(groupPosition+1)+"项的第"+(childPosition+1)+"", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }


    private void initialData() {
        adapter.childrenList1.add("苹果");
        adapter.childrenList1.add("西瓜");
        adapter.childrenList1.add("香蕉");
        adapter.childrenList2.add("青菜");
        adapter.childrenList2.add("黄瓜");
        adapter.childrenList2.add("豆苗");
        adapter.childrenList3.add("");
        adapter.childrenList3.add("");
        adapter.childrenList3.add("");

    }

}
然后是适配器部分,适配器要继承BaseExpandableListAdapter来写

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    public Map<String, List<String>> dataset = new HashMap<>();
    public List<String> childrenList1 = new ArrayList<>();
    public List<String> childrenList2 = new ArrayList<>();
    public List<String> childrenList3 = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private String[] parentList;

    private static final String TAG = "TAG";
    public MyExpandableListViewAdapter(Context context,String[] parentList) {
        this.context = context;
        this.parentList = parentList;
        dataset.put(parentList[0], childrenList1);
        dataset.put(parentList[1], childrenList2);
        dataset.put(parentList[2], childrenList3);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    //  获得某个父项的某个子项
    @Override
    public Object getChild(int parentPos, int childPos) {
        return dataset.get(parentList[parentPos]).get(childPos);
    }

    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return dataset.size();
    }

    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int parentPos) {
        return dataset.get(parentList[parentPos]).size();
    }

    //  获得某个父项
    @Override
    public Object getGroup(int parentPos) {
        return dataset.get(parentList[parentPos]);
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }

    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }

    //  按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //  获得父项显示的view
    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.parent_item, null);
        }
        view.setTag(R.layout.parent_item, parentPos);
        view.setTag(R.layout.child_item, -1);
        TextView text = (TextView) view.findViewById(R.id.parent_title);
        text.setText(parentList[parentPos]);
        return view;
    }

    //  获得子项显示的view
    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.child_item, null);
        }
        view.setTag(R.layout.parent_item, parentPos);
        view.setTag(R.layout.child_item, childPos);
        TextView text = (TextView) view.findViewById(R.id.child_title);
        text.setText(dataset.get(parentList[parentPos]).get(childPos));
        /*text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "点到了内置的textview", Toast.LENGTH_SHORT).show();
            }
        });*/
        return view;
    }

    //  子项是否可选中,如果需要设置子项的点击事件,需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
父布局的item布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/parent_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:text="这是父item"
        android:paddingLeft="30dp"
        android:layout_margin="5dp"/>
</LinearLayout>

子布局的item也是一个简单的文字

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/child_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:text="这是子item"
        android:layout_margin="5dp"/>
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值