Android ExpandableListView

Android ExpandableListView,第1张

概述AndroidExpandableListView控件的使用<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas 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

然后重写如下方法:

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

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

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

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

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

总结

以上是内存溢出为你收集整理的Android ExpandableListView全部内容,希望文章能够帮你解决Android ExpandableListView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1060163.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存