ExpandableListView是Listview的一个父类,在实现ExpandableListView中,最终要的就是实现它的适配器。适配器中的主要实现getGroupView和getChildView两个方法,这两个方法的最要是完成ExpandableListView和ListView的视图功能。
下面是实现的效果:
实现环境
- Linux下Android Studio
- Android 5.1 API 22
实现代码
布局文件:fragment_account.xml:
<LinearLayout 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:orientation="vertical"
tools:context="com.example.steam.app_mydemon_qq.AccountFragment">
<include layout="@layout/edit_layout" />
<include layout="@layout/account_menu_layout" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text=" 好友分组"
android:textSize="16dp" />
<com.example.steam.app_mydemon_qq.Mypeoplelist
android:id="@+id/ealistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:groupIndicator="@null"
android:background="@color/colorwhite">
</com.example.steam.app_mydemon_qq.Mypeoplelist>
</LinearLayout>
Mypeoplelist 是继承自ExpandableListView的一个自定义控件。
代码如下
Mypeoplelist .class:
package com.example.steam.app_mydemon_qq;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by steam on 15-12-8.
*/
public class Mypeoplelist extends ExpandableListView {
public List<String> groupList = new ArrayList<String>();
public List<List<String>> subListName=new ArrayList<List<String>>();
public List<List<Integer>> subListIcon = new ArrayList<List<Integer>>();
public Mypeoplelist(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
Initdata();
}
public Mypeoplelist(Contex