设为首页 加入收藏

TOP

Android UI控件----ExpandableListView的基本用法(一)
2017-10-13 10:31:04 】 浏览:2314
Tags:Android 控件 ----ExpandableListView 基本 用法

ExpandableListView介绍

ExpandableListView的引入

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView)。ExpandableListView允许有两个层次:一级列表中有二级列表。
比如在手机设置中,对于分类,有很好的效果。手机版QQ也是这样的效果。

使用ExpandableListView的整体思路

(1)给ExpandableListView设置适配器,那么必须先设置数据源。

(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter,需要重写里面的10个方法。
数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。
getChildView()和getGroupView()方法设置自定义布局。

(3)数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。

ExpandableListView的完整代码实现

(1)activity_main.xml:在里面放置一个ExpandableListView控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.smyhvae.expandablelistviewdemo.MainActivity">


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

</RelativeLayout>

(2)item_group.xml:一级列表的item的布局

<?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="wrap_content"
 android:background="#cccccc"
 android:orientation="horizontal">

    <TextView
 android:id="@+id/tv_group"
 android:layout_width="wrap_content"
 android:layout_height="30dp"
 android:gravity="center"
 android:text="group text"
 android:textColor="#000000"
        />

</LinearLayout>

(3)item_child.xml:二级列表的item的布局

<?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="wrap_content"
 android:gravity="center"
 android:orientation="horizontal">

    <ImageView
 android:id="@+id/iv_child"
 android:layout_width="30dp"
 android:layout_height="30dp"
 android:src="@mipmap/ic_launcher"/>

    <TextView
 android:id="@+id/tv_child"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="item text"
 android:textColor="#000000"/>

</LinearLayout>

(4)MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇android客户端从服务器端获取json.. 下一篇取代SharedPreferences的多进程解..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目