android之ExpandableListView

       ExpandableListView是一个two-level列表视图,一级目录可以展开显示出第二级目录,手机QQ联系人列表的实现效果就可以通过ExpandableListView来实现。现在来做一个ExpandableListView的Demo:

       首先,要准备两个布局文件groups.xml和childs.xml来分别表示一级目录和二级目录视图:

       一级目录groups.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/mytextview_groups"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:paddingLeft="40dp"
        android:gravity="center_vertical"
        />

</LinearLayout>

         二级目录childs.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/mytextview_childs"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:paddingLeft="10dp"
        android:gravity="center_vertical"
        />

</LinearLayout>

这里两级目录均只是显示一个TextView。接下来,main.xml布局文件如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

然后,需要提供Adapter来为可折叠列表提供数据和视图,这里实现MyExpandableListAdapter继承自BaseExpandableListAdapter基类,主要重写两个主要方法getChildView和getGroupView,具体实现如下:



/**MyExpandableListAdapter继承自BaseExpandableListAdapter,
 * 为ExpandableListView提供数据和视图,一级视图和二级视图均只显示
 * 一个TextView
 * @author Administrator
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	
	private Context context;
	
	//一级目录数据
	private List<String> groups;
	
	//二级目录数据
	private List<List<String>> childs;
	
	public MyExpandableListAdapter(Context context,List<String> groups,List<List<String>> childs){
		this.context = context;
		this.groups = groups;
		this.childs = childs;
	}

	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return childs.get(arg0).get(arg1);
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	/* (non-Javadoc)返回二级视图,包含一个TextView
	 */
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView childTextView = null;
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.childs, null);
		}
		childTextView = (TextView)convertView.findViewById(R.id.mytextview_childs);
		childTextView.setText(childs.get(groupPosition).get(childPosition));
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childs.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	/* (non-Javadoc)一级视图,也只包含一个TextView
	 */
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
 		TextView groupTextView = null;
 		if(convertView == null){
 			convertView = LayoutInflater.from(context).inflate(R.layout.groups, null);
 		}
 		groupTextView = (TextView)convertView.findViewById(R.id.mytextview_groups);
 		groupTextView.setText(groups.get(groupPosition));
		return convertView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
}

最后,Activity中实现如下:



public class ExpandableListViewDemo extends Activity {
	
	private ExpandableListView expandableListView = null;
	
	private List<String> groups = new ArrayList<String>();
	
	private List<List<String>> childs = new ArrayList<List<String>>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        expandableListView = (ExpandableListView)findViewById(android.R.id.list);
        groups.add("朋友");
        groups.add("同学");
        groups.add("同事");
        
        List<String> friends = new ArrayList<String>();
        friends.add("小明");
        friends.add("小红");
        friends.add("小强");
        childs.add(friends);
        
        List<String> classmates = new ArrayList<String>();
        classmates.add("甲");
        classmates.add("乙");
        classmates.add("丙");
        childs.add(classmates);
        
        List<String> colleagues = new ArrayList<String>();
        colleagues.add("A君");
        colleagues.add("B君");
        colleagues.add("C君");
        childs.add(colleagues);
        
        expandableListView.setAdapter(new MyExpandableListAdapter(ExpandableListViewDemo.this, groups, childs));
    }
}

上图:



      



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值