android可扩展列表,android ExpandableListView可扩展列表

本文介绍了如何使用自定义的ExpandableListAdapter实现带有图片和文字的列表项,并提供了详细的代码示例,包括创建适配器、定义布局文件及如何在Activity中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://leiwuluan.iteye.com/blog/1508356

先看一效果图、

列表中要有 图片和文字:

uid-20661656-id-4219726.html

所以我们要实现一个自定义的   适配器。

介绍一个类:BaseExpandableListAdapter

一看就知道是 适配器的一个基类了。

所以我们自定义的适配器要 继承它。

除了 完成这个 适配器,还要有两个自定义模板,分别   组和子列表的,单元模板。如下图:

uid-20661656-id-4219726.html

模板布局xml  要放在 layouts  下面。

main_tree_group.xml

Xml代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

xmlversion="1.0"encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="45dp"

android:background="@color/white"

android:gravity="center"

android:orientation="vertical">

xmlns:android=""

android:id="@+id/main_tree_title_id"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="20dip"

android:background="@color/white"

android:text="NoData"

android:textColor="@color/black"

android:textSize="20dp"

android:textStyle="bold"/>

LinearLayout>

uid-20661656-id-4219726.html

子列表模板文件

main_tree_child.xml

Xml代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

xmlversion="1.0"encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@color/white"

android:gravity="center_vertical"

android:orientation="horizontal"

android:paddingBottom="5dp"

android:paddingLeft="8dp"

android:paddingTop="8dp">

android:id="@+id/mainChildIcoId"

android:layout_width="50dp"

android:layout_height="50dp"

android:src="@drawable/person_icon"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/mainChildText1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginTop="5dp"

android:background="@color/white"

android:gravity="center_vertical"

android:text="CNoData"

android:textColor="@color/black"

android:textSize="16dp"/>

android:id="@+id/mainChildText2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:background="@color/white"

android:gravity="center_vertical"

android:text="13693668970"

android:textColor="@color/black"

android:textSize="12dp"/>

LinearLayout>

LinearLayout>

uid-20661656-id-4219726.html

然后写两个对  模板文件的    bean

main_tree_group.xml 对应 bean

Java代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

//父单元

classExpandableGroupHolder {

TextView title;

}

//父单元

class ExpandableGroupHolder {

TextView title;

}

main_tree_child.xml

Java代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

//单元类

classExpandableListHolder {

TextView nickName;

TextView phone;

ImageView ioc;

}

//单元类

class ExpandableListHolder {

TextView nickName;

TextView phone;

ImageView ioc;

}

现在来实现最重要的关结。  适配器

MainListExpandableListAdapter.java

我这里把 上面两个模板对应java bean 写成 自定义适配器的内部类。

Java代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

packagecom.main.apadter;

importjava.util.List;

importjava.util.Map;

importandroid.content.Context;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.BaseExpandableListAdapter;

importandroid.widget.ImageView;

importandroid.widget.TextView;

importcom.main.R;

publicclassMainListExpandableListAdapterextendsBaseExpandableListAdapter {

//单元类

classExpandableListHolder {

TextView nickName;

TextView phone;

ImageView ioc;

}

//父单元

classExpandableGroupHolder {

TextView title;

}

privateList> groupData;//组显示

privateList>> childData;//子列表

privateLayoutInflater mGroupInflater;//用于加载group的布局xml

privateLayoutInflater mChildInflater;//用于加载listitem的布局xml

//自宝义构造

publicMainListExpandableListAdapter(Context context, List> groupData, List>> childData) {

this.childData=childData;

this.groupData=groupData;

mChildInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mGroupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

//必须实现 得到子数据

@Override

publicObject getChild(intgroupPosition,intj) {

returnchildData.get(groupPosition).get(j);

}

@Override

publiclonggetChildId(intgroupPosition,intj) {

returngroupPosition;

}

@Override

publicintgetChildrenCount(inti) {

returnchildData.get(i).size();

}

@Override

publicObject getGroup(inti) {

returngroupData.get(i);

}

@Override

publicintgetGroupCount() {

returngroupData.size();

}

@Override

publiclonggetGroupId(inti) {

returni;

}

@Override

publicbooleanhasStableIds() {//行是否具有唯一id

returnfalse;

}

@Override

publicbooleanisChildSelectable(inti,intj) {//行是否可选

returnfalse;

}

@Override

publicView getGroupView(intgroupPosition,booleanflag, View convertView, ViewGroup viewgroup) {

ExpandableGroupHolder holder = null;//清空临时变量holder

if(convertView ==null) {//判断view(即view是否已构建好)是否为空

convertView = mGroupInflater.inflate(R.layout.main_tree_group, null);

holder = newExpandableGroupHolder();

holder.title=(TextView) convertView.findViewById(R.id.main_tree_title_id);

convertView.setTag(holder);

} else{//若view不为空,直接从view的tag属性中获得各子视图的引用

holder = (ExpandableGroupHolder) convertView.getTag();

}

String title=(String)this.groupData.get(groupPosition).get("title");

holder.title.setText(title);

notifyDataSetChanged();

returnconvertView;

}

@Override

publicView getChildView(intgroupPosition,intchildPosition,booleanisLastChild, View convertView,

ViewGroup viewgroup) {

ExpandableListHolder holder = null;

if(convertView ==null) {

convertView = mChildInflater.inflate(R.layout.main_tree_child, null);

holder = newExpandableListHolder();

holder.nickName = (TextView) convertView.findViewById(R.id.mainChildText1);

holder.ioc = (ImageView) convertView.findViewById(R.id.mainChildIcoId);

holder.phone = (TextView) convertView.findViewById(R.id.mainChildText2);

convertView.setTag(holder);

} else{//若行已初始化,直接从tag属性获得子视图的引用

holder = (ExpandableListHolder) convertView.getTag();

}

MapunitData=this.childData.get(groupPosition).get(childPosition);

holder.nickName.setText((String)unitData.get("nickName"));

holder.ioc.setImageResource((Integer) unitData.get("ico"));

holder.phone.setText((String)unitData.get("phone"));

returnconvertView;

}

}

package com.main.apadter;

import java.util.List;

import java.util.Map;

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 com.main.R;

public class MainListExpandableListAdapter extends BaseExpandableListAdapter {

//单元类

class ExpandableListHolder {

TextView nickName;

TextView phone;

ImageView ioc;

}

//父单元

class ExpandableGroupHolder {

TextView title;

}

private List> groupData;//组显示

private List>> childData;//子列表

private LayoutInflater mGroupInflater; //用于加载group的布局xml

private LayoutInflater mChildInflater; //用于加载listitem的布局xml

//自宝义构造

public MainListExpandableListAdapter(Context context, List> groupData, List>> childData) {

this.childData=childData;

this.groupData=groupData;

mChildInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mGroupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

//必须实现 得到子数据

@Override

public Object getChild(int groupPosition, int j) {

return childData.get(groupPosition).get(j);

}

@Override

public long getChildId(int groupPosition, int j) {

return groupPosition;

}

@Override

public int getChildrenCount(int i) {

return childData.get(i).size();

}

@Override

public Object getGroup(int i) {

return groupData.get(i);

}

@Override

public int getGroupCount() {

return groupData.size();

}

@Override

public long getGroupId(int i) {

return i;

}

@Override

public boolean hasStableIds() {//行是否具有唯一id

return false;

}

@Override

public boolean isChildSelectable(int i, int j) {//行是否可选

return false;

}

@Override

public View getGroupView(int groupPosition, boolean flag, View convertView, ViewGroup viewgroup) {

ExpandableGroupHolder holder = null; //清空临时变量holder

if (convertView == null) { //判断view(即view是否已构建好)是否为空

convertView = mGroupInflater.inflate(R.layout.main_tree_group, null);

holder = new ExpandableGroupHolder();

holder.title=(TextView) convertView.findViewById(R.id.main_tree_title_id);

convertView.setTag(holder);

} else { //若view不为空,直接从view的tag属性中获得各子视图的引用

holder = (ExpandableGroupHolder) convertView.getTag();

}

String title=(String)this.groupData.get(groupPosition).get("title");

holder.title.setText(title);

notifyDataSetChanged();

return convertView;

}

@Override

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,

ViewGroup viewgroup) {

ExpandableListHolder holder = null;

if (convertView == null) {

convertView = mChildInflater.inflate(R.layout.main_tree_child, null);

holder = new ExpandableListHolder();

holder.nickName = (TextView) convertView.findViewById(R.id.mainChildText1);

holder.ioc = (ImageView) convertView.findViewById(R.id.mainChildIcoId);

holder.phone = (TextView) convertView.findViewById(R.id.mainChildText2);

convertView.setTag(holder);

} else {//若行已初始化,直接从tag属性获得子视图的引用

holder = (ExpandableListHolder) convertView.getTag();

}

MapunitData=this.childData.get(groupPosition).get(childPosition);

holder.nickName.setText((String)unitData.get("nickName"));

holder.ioc.setImageResource((Integer) unitData.get("ico"));

holder.phone.setText((String)unitData.get("phone"));

return convertView;

}

}

接下来要做的就是  利用自定义的适配器。  添加盟数据进行显了。

1、建一个 xml  设样式并设id

Xml代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

xmlversion="1.0"encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:listSelector="@color/white"

android:orientation="vertical">

android:id="@+id/expandable_id"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/white"

android:drawSelectorOnTop="false"

android:listSelector="@color/white"/>

LinearLayout>

创建activity

Java代码 f83477f04be71993fceb62278cb92fb3.gif e9495c037be9e483aa735543c063c8a6.png

7cb29ee1c823d285bffd7ff42b62cd56.gif

publicclassMainActivityextendsActivity {

// 声明对象

privateMainListExpandableListAdapter adapter =null;

List> groups;

List>> childs;

ExpandableListView expandableListView;

privateFriendsDao friendsDao;

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

friendsDao=newFriendsDao(this,"ll1x.db",null,2);

//为ExpandableListView准备数据

groups = newArrayList>();

Mapgroup = newHashMap();

group.put("title","我的家人");

groups.add(group);

List> child1 = newArrayList>();

Cursor cursor = friendsDao.selectAll();

while(cursor.moveToNext()){

Mapchild1Data1 = newHashMap();

child1Data1.put("nickName", cursor.getString(cursor.getColumnIndex("nickName")));

child1Data1.put("phone", cursor.getString(cursor.getColumnIndex("phone")));

child1Data1.put("ico", R.drawable.icon);

child1.add(child1Data1);

}

childs = newArrayList>>();

childs.add(child1);

// 实例化ExpandableListView对象

expandableListView = (ExpandableListView) findViewById(R.id.expandable_id);

// 实例化ExpandableListView的适配器

adapter = newMainListExpandableListAdapter(getApplicationContext(), groups, childs);

// 设置适配器

expandableListView.setAdapter(adapter);

// 设置监听器

expandableListView.setOnChildClickListener(newOnChildClickListener() {

publicbooleanonChildClick(ExpandableListView parent, View v,

intgroupPosition,intchildPosition,longid) {

Log.d("test","GroupPosition is "+ groupPosition);

Log.d("test","ChildPosition is"+ childPosition);

returnfalse;

}

});

}

}

public class MainActivity extends Activity {

// 声明对象

private MainListExpandableListAdapter adapter = null;

List> groups;

List>> childs;

ExpandableListView expandableListView;

private FriendsDao friendsDao;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

friendsDao=new FriendsDao(this,"ll1x.db",null,2);

//为ExpandableListView准备数据

groups = new ArrayList>();

Mapgroup = new HashMap();

group.put("title", "我的家人");

groups.add(group);

List> child1 = new ArrayList>();

Cursor cursor = friendsDao.selectAll();

while(cursor.moveToNext()){

Mapchild1Data1 = new HashMap();

child1Data1.put("nickName", cursor.getString(cursor.getColumnIndex("nickName")));

child1Data1.put("phone", cursor.getString(cursor.getColumnIndex("phone")));

child1Data1.put("ico", R.drawable.icon);

child1.add(child1Data1);

}

childs = new ArrayList>>();

childs.add(child1);

// 实例化ExpandableListView对象

expandableListView = (ExpandableListView) findViewById(R.id.expandable_id);

// 实例化ExpandableListView的适配器

adapter = new MainListExpandableListAdapter(getApplicationContext(), groups, childs);

// 设置适配器

expandableListView.setAdapter(adapter);

// 设置监听器

expandableListView.setOnChildClickListener(new OnChildClickListener() {

public boolean onChildClick(ExpandableListView parent, View v,

int groupPosition, int childPosition, long id) {

Log.d("test", "GroupPosition is " + groupPosition);

Log.d("test", "ChildPosition is" + childPosition);

return false;

}

});

}

}

ok 可了。可以放到项目当去了。

大小: 66.3 KB

大小: 96.3 KB

大小: 7.1 KB

大小: 14.5 KB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值