二级菜单——ExpandableListView以及用RecyclerView实现

本文介绍了如何使用RecyclerView实现二级菜单功能,对比了ExpandableListView的优缺点。通过详细步骤展示了使用RecyclerView创建适配器,包括创建ViewHolder、绑定数据、处理列表项类型等,实现了类似ExpandableListView的效果。
摘要由CSDN通过智能技术生成

前言

二级菜单这个功能,相信很多app都需要这个功能,而我最近的项目中也有这样的需求。正常情况下,快捷的实现方式是使用Android提供的二级菜单控件——ExpandableListView,并编写相应的adapter,继承自BaseExpandableListAdapter即可。而非正常情况下,就是自己去实现这个二级菜单控件功能,而实现的基础就是RecyclerView。
优缺点
ExpandableListView:优点是便捷,逻辑处理Android系统已经都处理好了,很容易实现这个功能;缺点是都封装好了,可扩展性不高。
RecyclerView:优点就是扩展性高;缺点当然就是这些扩展性都需要自己实现,实现起来稍微费点时间。
但是呢,我们不怕麻烦,为了以后的代码有好的可扩展性,还是选择走这个非正常的情况。。。。

ExpandableListView,了解一下

在开始之前,我们也稍稍了解一下Android系统提供的这个二级菜单控件,这样可以更好的让我们了解实现二级菜单列表的原理,更好的开展我们的自定义的二级菜单列表。

ExpandableListView 是什么?

官方给出的解释是:
A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

简单翻译一下就是:
一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。

从上面的翻译,我们就可以了解到,其实二级菜单列表与一级菜单列表一样,都是通过Adapter来提供数据源的,因而实际使用的时候主要也是实现这个Adapter中的数据关联逻辑即可。

所以,简单的实现ExpandableListView功能来了解这个控件。

1 . 定义布局文件,这里就不多说了,类似于ListView,就是一个ExpandableListView控件

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

2 . 定义数据,分组的数据是个一维数组,子列表的数据是个二维数组——子列表依附于某个分组,本身还有索引,所以要定义成二维的。

public String[] groups = {
  "A", "B", "C", "D"};
    public String[][] children = {
            {
  "A1", "A2"},
            {
  "B1"},
            {
  "C1", "C2", "C3"},
            {
  "D1", "D2", "D3", "D4", "D5", "D6"}
    };

3 . 定义分组的视图和子选项的视图,就像定义ListView的item布局一样,不过这里是要定义两个布局分别显示组和子项,用最简单的 TextView显示文字即可,不再列代码。
4 . 最后就是关键的一步,继承BaseExpandableListAdapter,实现自定义的Adapter。直接上代码吧,在代码中有解释每一个方法的作用。

    // 获得组数
    @Override
    public int getGroupCount() {
        return groups.length;
    }

    // 获得组的子项个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    // 获得某组数据
    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    // 获得指定子项
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    // 获取组ID, 这个ID必须是唯一的
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 获取子项ID, 这个ID必须是唯一的
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们。
    @Override
    public boolean hasStableIds() {
        return true;
    }

    // 获取组视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_expand_group, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_expand_group);
            convertView.setTag(groupVi
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,以下是一个 ExpandableListView 二级分栏的例子: 首先在布局文件中定义 ExpandableListView 和 Adapter: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 定义自定义的 ExpandableListAdapter --> ``` 然后在 Activity 中设置 Adapter 并为 ExpandableListView 设置点击事件: ```java public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private ExpandableListAdapter adapter; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 准备数据 prepareListData(); // 设置 Adapter adapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); expandableListView.setAdapter(adapter); // 设置点击事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } // 准备数据 private void prepareListData() { listDataHeader = new ArrayList<>(); listDataChild = new HashMap<>(); // 添加一级分栏 listDataHeader.add("Fruits"); listDataHeader.add("Vegetables"); // 添加二级分栏 List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); List<String> vegetables = new ArrayList<>(); vegetables.add("Carrot"); vegetables.add("Tomato"); vegetables.add("Potato"); // 将二级分栏添加到对应的一级分栏下 listDataChild.put(listDataHeader.get(0), fruits); listDataChild.put(listDataHeader.get(1), vegetables); } } ``` 最后是 ExpandableListAdapter 的实现: ```java public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this.context = context; this.listDataHeader = listDataHeader; this.listDataChild = listDataChild; } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView lblListHeader = convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView txtListChild = convertView.findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 其中 `list_group.xml` 和 `list_item.xml` 分别是一级分栏和二级分栏的布局文件,可以根据需要自行定义。 以上就是 ExpandableListView 二级分栏的例子,希望能对你有所帮助!
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值