Android: ExpandableListView 二级列表 使用教程

这篇具有很好参考价值的文章主要介绍了Android: ExpandableListView 二级列表 使用教程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

相关属性

  • android:childDivider:指定各组内子类表项之间的分隔条,图片不会完全显示, 分离子列表项的是一条直线
  • android:childIndicator:显示在子列表旁边的Drawable对象,可以是一个图像
  • android:groupIndicator:显示在组列表旁边的Drawable对象,可以是一个图像
  • android:childIndicatorEnd:子列表项指示符的结束约束位置
  • android:childIndicatorLeft:子列表项指示符的左边约束位置
  • android:childIndicatorRight:子列表项指示符的右边约束位置
  • android:childIndicatorStart:子列表项指示符的开始约束位置
  • android:indicatorEnd:组列表项指示器的结束约束位置
  • android:indicatorLeft:组列表项指示器的左边约束位置
  • android:indicatorRight:组列表项指示器的右边约束位置
  • android:indicatorStart:组列表项指示器的开始约束位置

实现步骤

  1. 扩展BaseExpandableListAdpter实现ExpandableAdapter。
  2. 使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter
  3. 使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter

本节示例使用的是第一个,扩展BaseExpandableListAdpter,我们需要重写该类中的相关方法, 下面我们通过一个代码示例来体验下!

  1. 定义 Layout 样式
    先制定Group组的样式 Layout (item_exlist_group.xml)
    再制定 每个组 下面项 的 样式(item_exlist_item.xml)
  2. 设置 数据适配器
    先设置 Group 类 和 Item 类 存放数据
    然后自定义数据适配器 实现抽象类 BaseExpandableListAdapter (MyBaseExpandableListAdapter.java)
  3. 设置布局文件:activity_main.xml
  4. 最后 MainActivity.java 代码

BaseExpandableListAdpter

代码片段

item_exlist_group.xml

<?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"
    android:orientation="horizontal"
    android:padding="5dp"
    >
    <!--
        设置组的样式

        textStyle:
        Android 提供了三个样式
        1 normal 正常字体
        2 bold 粗体
        3 italic 斜体

    -->
    <TextView
        android:id="@+id/tv_group_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:paddingStart="30dp"
        android:text="@string/group_name"
        android:textStyle="bold"
        android:textSize="25sp" />

</LinearLayout>

item_exlist_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--设置 组 里面的项 内容排布-->
<?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"
    android:orientation="horizontal"
    android:padding="3dp"
    android:background="@color/ItemColor"
    >
    <ImageView
        android:id="@+id/image_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_baseline_camera_24"
        />

    <TextView
        android:id="@+id/tv_bookName"

        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="15dp"
        android:focusable="false"
        android:text="@string/book_name"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/tv_bookPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textStyle="bold"
        android:textSize="17sp"
        android:layout_marginTop="18dp"

        />
</LinearLayout>

MyBaseExpandableListAdapter.java
设置适配器

  1. 设置组类

    Group.java

package com.example.expandablelistviewstudy;

/**
 * 设置 组
 */
public class Group {
    private String gName;

    private Group() {
    }

    public Group(String gName) {
        this.gName = gName;
    }

    public String getGName() {
        return gName;
    }

    public void setGName(String gName) {
        this.gName = gName;
    }
}

  1. 设置子项类

    Item.java

package com.example.expandablelistviewstudy;

import android.widget.ImageView;

import java.math.BigDecimal;

/**
 * 设置 组下面的子项 里的内容
 */
public class Item {

    private int iId;
    private String iName;
    private BigDecimal bookPrice;

    public Item(int iId, String iName,BigDecimal bookPrice) {
        this.iId = iId;
        this.iName = iName;
        this.bookPrice = bookPrice;
    }


    public BigDecimal getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(BigDecimal bookPrice) {
        this.bookPrice = bookPrice;
    }

    public int getIId() {
        return iId;
    }

    public String getIName() {
        return iName;
    }

    public void setIId(int iId) {
        this.iId = iId;
    }

    public void setIName(String iName) {
        this.iName = iName;
    }
}


设置数据适配器
MyBaseExpandableListAdapter.java

package com.example.expandablelistviewstudy;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * MyBaseExpandableListAdapter.java
 * 设置 数据适配器
 * 自定义数据适配器 实现抽象类 BaseExpandableListAdapter
 */
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
    //组
    private ArrayList<Group> gGroups ;
    //子项
    private ArrayList<ArrayList<Item>> iItems;
    //上下文
    private Context mContext;

    //构造器
    public MyBaseExpandableListAdapter(Context mContext, ArrayList<Group> gGroups, ArrayList<ArrayList<Item>> iItems ){

        this.mContext = mContext;
        this.gGroups = gGroups;
        this.iItems = iItems;

    }
    @Override
    public int getGroupCount() {
        //获取所有分组个数
        return this.gGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //获取 指定 (groupPosition)分组的  子项 个数
        //因为 iItems 是由一个ArrayList<ArrayList<Item>> 就相当于  二维数组  [] []
        //[0] [0]
        //[1] [1]
        //所以就一一对应 当我们需要找 第一个组里面的 子项的时候
        //第一个组 下标就是  0   然后 我们就可以获取iItem 在 0,0 位置的一共由多少个个数
        return iItems.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        //获取指定(groupPosition) 组 对象

        return gGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //获取指定(groupPosition)分组的 (childPosition)子项 对象

        return iItems.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        //获取 指定 组 Id
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        //获取 指定 (groupPosition)组的指定 (childPosition)子项 Id
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //如果返回true,表示子项和组的ID始终表示一个固定的组件对象
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //获取指定(groupPosition)组的 view 组件
        ViewHolderGroup groupHolder ;

        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(groupHolder);
        }else{
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.tv_group_name.setText(gGroups.get(groupPosition).getGName());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //获得指定(groupPosition)组中的(childPosition)子项的 view 组件
        ViewHolderItem itemHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_item,parent,false);
            itemHolder = new ViewHolderItem();
            itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_bookName);
            itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.image_icon);
            itemHolder.tv_bookPrice =(TextView) convertView.findViewById(R.id.tv_bookPrice);
            convertView.setTag(itemHolder);

        }else{

            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        itemHolder.img_icon.setImageResource(iItems.get(groupPosition).get(childPosition).getIId());
        itemHolder.tv_name.setText(iItems.get(groupPosition).get(childPosition).getIName());
        itemHolder.tv_bookPrice.setText(iItems.get(groupPosition).get(childPosition).getBookPrice() + "元");

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //项是否可以被选中
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_group_name;
    }

    private static class ViewHolderItem{
        private ImageView img_icon;
        private TextView tv_name;
        private TextView tv_bookPrice;

    }
}


activity_main.xml

<?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/exList_books"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:childDivider="#F1E7E7"

        />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.expandablelistviewstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Group> groups = null;
    private  ArrayList<ArrayList<Item>> iItems = null;

    private ArrayList<Item> item = null;
    private Context mContext;
    private ExpandableListView exList_books;
    private MyBaseExpandableListAdapter myAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;
        exList_books = findViewById(R.id.exList_books);
        /**
         * 书本分组
         */
        groups = new ArrayList<>();
        groups.add(new Group("文学"));
        groups.add(new Group("哲学"));
        groups.add(new Group("其它"));

        /**
         * 书本分组的子项
         */
        iItems = new ArrayList<>();
        item = new ArrayList<>();

        //文学
        item.add(new Item(R.drawable.snipaste_eegnhya, "额尔古纳河右岸",new BigDecimal("19.00")));
        iItems.add(item);

        //哲学
        item = new ArrayList<>();
        item.add(new Item(R.drawable.snipaste_ttlvt, "天堂旅行团",new BigDecimal("30.00")));
        iItems.add(item);

        //其它
        item = new ArrayList<>();
        item.add(new Item(R.drawable.snipaste_ybygxmb, "云边有个小卖部",new BigDecimal("40.00")));
        iItems.add(item);

        myAdapter = new MyBaseExpandableListAdapter(mContext,groups,iItems);
        exList_books.setAdapter(myAdapter);


        exList_books.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(mContext, "你点击了:" + iItems.get(groupPosition).get(childPosition).getIName(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

Android: ExpandableListView 二级列表 使用教程文章来源地址https://www.toymoban.com/news/detail-417308.html

到了这里,关于Android: ExpandableListView 二级列表 使用教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 导航栏以及二级菜单栏(下拉列表)的制作

           作为新手小白,在我们熟悉了HTML , CSS,JS的功能和语法之后,Web前端开发中,更重要的还有界面的美化,主要依据CSS的庞大功能来实现,今天我来给大家分享的是,利用html代码来实现横向导航栏以及下拉菜单列表的实现。 下面是导航栏:   带有下拉列表的导航栏:

    2024年02月03日
    浏览(31)
  • Android 使用 RecyclerView 创建动态列表

    记录一下学习过程,RecyclerView 创建动态列表效果如下: Recyclerview 认识 Recyclerview 库: Recyclerview 指南: RecyclerView 可以显示大型数据集,通过回收有限数量的视图可以有效地滚动这些数据集,同时最大限度减少内存用量。 在实例化 ViewHolder 视图时可以定义单击侦听器。 Recy

    2023年04月27日
    浏览(37)
  • Android 之 Spinner (列表选项框)的基本使用

    本来本节是想给大家介绍一个Gallery(画廊)的一个控件的,后来想想还是算了,因为 在Android 4.1后就已经被弃用了,尽管我们可以通过兼容不来使用Gallery,不过想想 还是算了,因为Gallery在每次切换图片的时候,都需要重新创建视图,这样无疑会造成 很大资源浪费!我们可以通

    2024年02月10日
    浏览(43)
  • 如何在 Android 应用中使用 RecyclerView 实现一个列表显示,并实现点击事件?

    首先,需要在项目的 build.gradle 文件中添加 RecyclerView 的依赖: 接下来,在布局文件中添加 RecyclerView: 接着,需要创建一个 Adapter 类,用于将数据绑定到 RecyclerView 上,如下所示: 在 onBindViewHolder() 方法中,我们可以将数据绑定到 ViewHolder 中的视图上。 需要注意的是,在 V

    2024年02月05日
    浏览(41)
  • Android PopupWindow+RecyclerView 实现二级联动筛选

    这篇文章主要的功能是利用 PopupWindow 和  RecyclerView 实现条件筛选包括二级联动筛选,主要是仿小红书里的筛选功能而写的一个 Demo 效果如下,代码通俗易懂,保姆级教程 这里我模拟实际接口返回的数据而准备的数据源,在工程目录下新建 assets 资源文件,在新建一个JsonDat

    2024年02月13日
    浏览(63)
  • android excludeFromRecents将activity在最近的使用的应用程序列表中不显示

    excludeFromRecents 是Android应用程序清单文件(AndroidManifest.xml)中的一个属性,用于控制应用程序是否在最近使用的应用程序列表中显示。通过将 excludeFromRecents 属性设置为 true ,可以将应用程序从最近使用的应用程序列表中排除。 以下是将应用程序排除在最近使用的应用程序列

    2024年02月15日
    浏览(48)
  • android 12.0Settings去掉二级三级菜单搜索功能

    在12.0由于客户定制开发需求,需要去掉Settings里面的搜索功能,主页面的搜索功能,在前面的章节已经讲了 这里需要去掉二级三级菜单的搜索功能,需要从搜索功能流程分析去掉搜索功能 在系统Setting的搜索框源码中, 二级三级菜单就需要一步步跟源码来根据原理实现 每一

    2024年02月06日
    浏览(37)
  • uniapp打包成Android时,使用uni.chooseLocation在App端显示的地址列表是空白的解决办法

    前言: 最近在做项目的时候出现了一个很无解的问题,问了很多人,找了很多的资料,都没有解决.最后在一个突然的机会,我发现了一个小线索.(继续往下看!) 问题描述: uniapp在打包成app后使用uni.chooseLocation后,在app端,选择地址的时候,你会发现他一直都是在转圈的状态,但是真机调试

    2024年02月02日
    浏览(45)
  • springboot 高级教程 jetcache 二级缓存用法

    多级缓存实际上是在二级缓存的基础上,为了更好地提高系统的性能和可靠性,更适用于大型分布式系统的应用场景。 使用多级缓存的原因包括: 数据缓存分层:不同级别的缓存可以被用来缓存不同类型、不同频率访问的数据。在这种情况下,系统会先在本地缓存中寻找数

    2024年02月08日
    浏览(31)
  • Android10 Settings系列(三)根据需求动态添加删除一级菜单、二级菜单的设置项

    当时遇到定制需求,需要根据实际需要隐藏Settings的菜单项,于是开始了寻找方法 在看了一下源码,经过尝试后,确认生效后,就简单说明一下Settings中布局中主要组成元素 Settings中的菜单项是由 PreferenceScreen 和Preference组成的。其中PreferenceScreen 类似于我们平常使用布局中的

    2024年02月14日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包