【安卓笔记】ExpandableListView的使用

实现效果:

即可伸展的ListView



其实跟普通的ListView使用没啥区别,只是ListView改为了ExpandableListView,另外适配器由BaseAdapter也换成了BaseExpandableListAdapter。

步骤:

1.编写布局文件。

分为三个,分别是主布局,group分组布局,group item布局:
activity_main.xml:
<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"
    tools:context="com.example.expandablelistviewdemo.MainActivity" >
    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#000"
        android:dividerHeight="2.5dp"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft" >
    </ExpandableListView>
</RelativeLayout>

list_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="vertical" >
    <TextView
        android:id="@+id/listTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingTop="10dp"
        android:textColor="#A4C739" />
</LinearLayout>
list_item.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="vertical" >
    <TextView
        android:id="@+id/expandedListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="10dp" />
</LinearLayout>
2.编写适配器代码:

package com.example.expandablelistviewdemo;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyExpandableListAdapter extends BaseExpandableListAdapter
{
	private Context mContext;
	private List<String> expandableListTitle;
	private HashMap<String, List<String>> expandableListDetail;
	
	public MyExpandableListAdapter(List<String> expandableListTitle,HashMap<String,List<String>> expandableListDetail,Context mContext)
	{
		this.expandableListDetail = expandableListDetail;
		this.expandableListTitle = expandableListTitle;
		this.mContext = mContext;
	}
	
	@Override
	public int getGroupCount()//分组数
	{
		return this.expandableListTitle.size();
	}
	@Override
	public int getChildrenCount(int groupPosition)//分组内的item数
	{
		return this.expandableListDetail.get(expandableListTitle.get(groupPosition)).size();
	}
	@Override
	public Object getGroup(int groupPosition)//获取分组数据
	{
		return this.expandableListTitle.get(groupPosition);
	}
	@Override
	public Object getChild(int groupPosition, int childPosition)//获取第几分组第几个item的数据
	{
		return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition);
	}
	@Override
	public long getGroupId(int groupPosition)
	{
		return groupPosition;
	}
	@Override
	public long getChildId(int groupPosition, int childPosition)
	{
		return childPosition;
	}
	@Override
	public boolean hasStableIds()
	{
		return false;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent)
	{
		String data = this.expandableListTitle.get(groupPosition);
		if(convertView == null)
		{
			convertView = View.inflate(mContext,R.layout.list_group, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.listTitle);
		tv.setText(data);
		return convertView;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent)
	{
//		String data = this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition);
		String data = (String) this.getChild(groupPosition, childPosition);
		if(convertView == null)
		{
			convertView = View.inflate(mContext, R.layout.list_item, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.expandedListItem);
		tv.setText(data);
		return convertView;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition)
	{
		return true;
	}
}

可以看到,适配器中有两个getView的方法,分别去构造group和child的view。

3.构造数据源
package com.example.expandablelistviewdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExpandableListDataPump
{
	public static HashMap<String, List<String>> getData()
	{
		HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
		List<String> technology = new ArrayList<String>();
		technology.add("Beats sued for noise-cancelling tech");
		technology.add("Wikipedia blocks US Congress edits");
		technology.add("Google quizzed over deleted links");
		technology.add("Nasa seeks aid with Earth-Mars links");
		technology.add("The Good, the Bad and the Ugly");
		List<String> entertainment = new ArrayList<String>();
		entertainment.add("Goldfinch novel set for big screen");
		entertainment.add("Anderson stellar in Streetcar");
		entertainment.add("Ronstadt receives White House honour");
		entertainment.add("Toronto to open with The Judge");
		entertainment.add("British dancer return from Russia");
		List<String> science = new ArrayList<String>();
		science.add("Eggshell may act like sunblock");
		science.add("Brain hub predicts negative events");
		science.add("California hit by raging wildfires");
		science.add("Rosetta's comet seen in close-up");
		science.add("Secret of sandstone shapes revealed");
		expandableListDetail.put("TECHNOLOGY NEWS", technology);
		expandableListDetail.put("ENTERTAINMENT NEWS", entertainment);
		expandableListDetail.put("SCIENCE & ENVIRONMENT NEWS", science);
		return expandableListDetail;
	}
}

4.编写主界面的代码
package com.example.expandablelistviewdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MainActivity extends Activity
{
	private ExpandableListView elv = null;
	private MyExpandableListAdapter adapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		elv = (ExpandableListView) findViewById(R.id.elv);
		
		final HashMap<String,List<String>> itemData = ExpandableListDataPump.getData();
		final List<String> title = new ArrayList<String>(itemData.keySet());
		adapter = new MyExpandableListAdapter(title,itemData,this);
		
		elv.setAdapter(adapter);
		
		//去掉箭头
//		elv.setGroupIndicator(null);
		
		//收缩
		elv.setOnGroupCollapseListener(new OnGroupCollapseListener()
		{
			
			@Override
			public void onGroupCollapse(int groupPosition)
			{
				Toast.makeText(MainActivity.this,title.get(groupPosition)+" group collapse..... ", 0).show();
			}
		});
		//伸展
		elv.setOnGroupExpandListener(new OnGroupExpandListener()
		{
			@Override
			public void onGroupExpand(int groupPosition)
			{
				Toast.makeText(MainActivity.this,title.get(groupPosition)+" group expand... ", 0).show();
			}
		});
		//子条目点击
		elv.setOnChildClickListener(new OnChildClickListener()
		{
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id)
			{
				Toast.makeText(MainActivity.this,itemData.get(title.get(groupPosition)).get(childPosition)+" click", 0).show();
				return false;
			}
		});
		
	}
}

ok,完成。
这里需要注意的是ExpandableListView有多个监听器,group收缩和伸展以及子条目点击等等,按需监听。
另外,默认情况下显示的时候有个箭头,你可以通过setGroupIndicator方法设置你需要的标识。





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解安卓开发 ExpandableListView使用示例,以下是一个简单的示例: 1. 定义适配器类 ``` public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupTitles; // 父分组数据 private HashMap<String, List<String>> mChildData; // 子分组数据 public ExpandableListAdapter(Context context, List<String> groupTitles, HashMap<String, List<String>> childData) { mContext = context; mGroupTitles = groupTitles; mChildData = childData; } @Override public int getGroupCount() { return mGroupTitles.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return mGroupTitles.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_group, null); TextView title = (TextView) view.findViewById(R.id.group_title); title.setText(mGroupTitles.get(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_child, null); TextView title = (TextView) view.findViewById(R.id.child_title); title.setText(mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 2. 定义布局文件 list_item_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="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="20dp" android:paddingBottom="20dp"> <TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> ``` list_item_child.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="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="15dp" android:paddingBottom="15dp"> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout> ``` 3. 在 Activity 中使用 ExpandableListView ``` public class MainActivity extends AppCompatActivity { private ExpandableListView mExpandableListView; private ExpandableListAdapter mAdapter; private List<String> mGroupTitles; private HashMap<String, List<String>> mChildData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据 initData(); // 初始化 ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view); mAdapter = new ExpandableListAdapter(this, mGroupTitles, mChildData); mExpandableListView.setAdapter(mAdapter); // 设置子项点击监听器 mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, "您点击了" + mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return true; } }); } private void initData() { mGroupTitles = new ArrayList<>(); mGroupTitles.add("计算机"); mGroupTitles.add("自然科学"); mGroupTitles.add("哲学"); List<String> computers = new ArrayList<>(); computers.add("Java"); computers.add("Python"); computers.add("C++"); computers.add("PHP"); computers.add("JavaScript"); computers.add("HTML/CSS"); computers.add("SQL"); List<String> sciences = new ArrayList<>(); sciences.add("物理"); sciences.add("化学"); sciences.add("生物"); sciences.add("地理"); sciences.add("天文"); List<String> philosophies = new ArrayList<>(); philosophies.add("伦理学"); philosophies.add("形而上学"); philosophies.add("认识论"); philosophies.add("逻辑学"); philosophies.add("美学"); mChildData = new HashMap<>(); mChildData.put(mGroupTitles.get(0), computers); mChildData.put(mGroupTitles.get(1), sciences); mChildData.put(mGroupTitles.get(2), philosophies); } } ``` 这样,您就可以展示一个带父分组和子分组的 ExpandableListView ,同时设置子项点击监听器,实现相应的功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值