Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ExpandableListView實例Demo

Android ExpandableListView實例Demo

編輯:關於Android編程

前幾篇文章介紹了Listview,但在實際開發中也經常會用到多層的Listview來展示數據,比如qq中的好友展示,所以這張來了解一下ExpandableListview,基本思想與Listview大致是相同的,所以用起來會比較方便。

實現效果圖:

\

程序代碼:

\

布局文件:

activity_main.xml:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">
group和child共同使用的布局(當然也可以使用不同的布局來實現),這的布局比較簡單,只有一個Textview:

list_item:




    




MainActivity:

package com.listviewdemo_expandablelistview;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
	private ExpandableListView listView;
	private List group;
	private List> child;
	private MyAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (ExpandableListView) findViewById(R.id.expandableListView);
		/**
		 * 初始化數據 
		 */
		initData();
		adapter = new MyAdapter(this,group,child);
		listView.setAdapter(adapter);
		
	}

	private void initData() {
		group = new ArrayList();
		child = new ArrayList>();
		addInfo("北京",new String[]{"朝陽","海澱","東城區","西城區"});
		addInfo("河北", new String[]{"邯鄲","石家莊","邢台"});
		addInfo("廣東", new String[]{"廣州","深圳","珠海"});
	}
	
	/**
	 * 添加數據信息
	 * @param g
	 * @param c
	 */
	private void addInfo(String g,String[] c) {
		group.add(g);
		List list = new ArrayList();
		for (int i = 0; i < c.length; i++) {
			list.add(c[i]);
		}
		child.add(list);
	}

}

MyAdapter:

package com.listviewdemo_expandablelistview;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
 * expandableListView適配器
 *
 */
public class MyAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List group;
	private List> child;

	public MyAdapter(Context context, List group,
			List> child) {
		this.context = context;
		this.group = group;
		this.child = child;
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return child.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return child.get(childPosition).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;
	}
	
	/**
	 * 顯示:group
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_item, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView
					.findViewById(R.id.textView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.textView.setText(group.get(groupPosition));
		holder.textView.setTextSize(25);
		holder.textView.setPadding(36, 10, 0, 10);
		return convertView;

	}
	
	/**
	 * 顯示:child
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_item, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView
					.findViewById(R.id.textView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.textView.setText(child.get(groupPosition).get(childPosition));
		holder.textView.setTextSize(20);
		holder.textView.setPadding(72, 10, 0, 10);
		return convertView;
	}

	class ViewHolder {
		TextView textView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}

現在CSDN博客中加入鏈接需要審核後才能發表,所以需要源碼的可以到我上傳的資源中查找。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved