ExpandableListView--可展开的列表组件

本文介绍了Android中可展开的列表组件ExpandableListView的使用,包括其与ListView的关系、用法以及如何通过自定义ExpandableListAdapter来实现列表项。通过扩展BaseExpandableListAdapter、使用SimpleExpandableListAdapter或SimpleCursorTreeAdapter等方式实现ExpandableListAdapter,并详细说明了getGroupView()、getChildrenCount()和getChildView()等关键方法的作用。
摘要由CSDN通过智能技术生成

为培养手感,最近在前端学习期间,总会学习疯狂Android讲义上的小Demo,用来培养Android手感,以免手生,今天联系ExpandableListView可展开的列表组件,这种效果可以说到处可见了,咋感觉这种效果会有点复杂,,不过只要理清思路,这是相当简单的.下面就讲解一下了:

ExpandableListView是ListView的子类,他可以说是在普通的ListView的基础上进行扩展的,他把应用中的列表项分为几组,每组里又可包含多个列表项.所以说ExpandableListView用法和ListView的用法非常相似,只是ExpandableListView所显示的列表项应该有ExpandableListAdapter提供.ExpandableListAdapter也是一个接口.它和Adapter也是类似的,实现ExpandableListAdapter有3种常用方式.

(1)扩展BaseExpandableListAdapter实现ExpandableListAdapter.

(2)使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter

(3)使用SimpleCursorTreeAdapter将Cursor中的数据包装成ExpandableListAdapter.

下面就是通过自定义ExpandableListAdapter为ExpandableListView提供列表项,那么先看该Demo的布局文件:

<?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="vertical"
    >
    <ExpandableListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
上面布局就相当简单了,就是在LinearLayout中定义了一个ExpandableListView,下面就是看在程序内怎么进行设置父组件和子组件的,如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(sav
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值