ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android ExpandableListView

2020-09-16 20:02:21  阅读:336  来源: 互联网

标签:return int groupPosition param ExpandableListView convertView lsChildData Androi


Android ExpandableListView

控件的使用

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expand_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

首先,添加ExpandableListView到主页面中。

数据源设置

//用于 Item Group 数据源
private List<String> lsGroupData = new ArrayList<>();
//用于 Item Child 数据源
private List<List<String>> lsChildGroup = new ArrayList<>();

// 初始换数据
private void initData() {
    int groupCount = 10;
    for (int i = 0; i < groupCount; i++) {
        lsGroupData.add("Item - " + i);
        int childCount = new Random().nextInt(30);
        List<String> lsChildData = new ArrayList<>();
        for (int i1 = 0; i1 < childCount; i1++) {
            lsChildData.add("item - " + i + " - " + i1);
        }
        lsChildGroup.add(lsChildData);
    }
}

适配器 代码

public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> lsGroupData;
    private List<List<String>> lsChildData;
    /**
     * 构造函数
     * @param context
     * @param lsGroupData 存储 关于 Item Group的全部数据
     * @param lsChildData 存储关于 Item Child的全部数据
     */
    MyAdapter(Context context, List<String> lsGroupData, List<List<String>> lsChildData) {
        this.context = context;
        this.lsGroupData = lsGroupData;
        this.lsChildData = lsChildData;
    }
    /**
     * 获取 Group 大小
     * @return
     */
    @Override
    public int getGroupCount() {
        return lsGroupData.size();
    }
    /**
     * 获取 Child 大小
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return lsChildData.get(groupPosition).size();
    }
    /**
     * 获取 指定 Position 下的Group 数据
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return lsGroupData.get(groupPosition);
    }
    /**
     * 获取 指定 Position 下的Child 数据
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return lsChildData.get(groupPosition).get(childPosition);
    }
    /**
     * 获取Group ID
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    /**
     * 获取Child ID
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    /**
     * 具有稳定的ID
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return false;
    }
    /**
     * 获取 Group 项的 View
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group, null);
        }
        TextView txtGroupName = convertView.findViewById(R.id.txtGroupName);
        txtGroupName.setText(lsGroupData.get(groupPosition));
        TextView txtItemCount = convertView.findViewById(R.id.txtItemsCount);
        txtItemCount.setText(String.valueOf(lsChildData.get(groupPosition).size()));
        return convertView;
    }
    /**
     * 获取 Child 项的 View
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_child, null);
        }
        TextView txtChildName = convertView.findViewById(R.id.txtItemName);
        txtChildName.setText(lsChildData.get(groupPosition).get(childPosition));
        txtChildName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, lsChildData.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

这部分就是适配器代码。

我们来分析一下:

首先,我们将我们的适配器类继承与BaseExpandableListAdapter

然后重写如下方法:

需要被重写的方法(图中全部方法)

我们重写下面所有的方法。

相关的方法注释在上面的代码中由具体写到。

最后,我们来看一下效果图:

Android ExpandableListView 效果图

这样,ExpandableListView的使用就到这里了。

本控件学习自:https://www.jianshu.com/p/8bc7c8e97afc

标签:return,int,groupPosition,param,ExpandableListView,convertView,lsChildData,Androi
来源: https://www.cnblogs.com/cao-1/p/13681053.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有