Android ExpendableListView的使用

越研究越发现紧紧是一个listview就有很多需要学习的地方,今天研究了下ExpandableListView,下面就来介绍下如何使用吧。也许有些人还不知道这个的效果,先上个效果图吧



点击某一项,下面会弹出相应的列表,类似QQ好友列表界面。


废话不多说 ,直接上代码

主布局就一个ExpandableListView

<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" >

    <ExpandableListView
        android:id="@+id/exlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true" />
</RelativeLayout>

主布局item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tvparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:textSize="22sp"
        android:text="TextView" />

    <ImageView
        android:id="@+id/ivfx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_browser" />

</RelativeLayout>
下面是展开的布局

<?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/tvchild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:textSize="22sp"
        android:textColor="#ff0000"
        android:text="TextView" />

</LinearLayout>
这里我对字体颜色进行了区分 ,方便区分哪个是主布局,哪个是子布局

接下来就是最重要的activity的使用了

public class MainActivity extends Activity {
	private List<String> group=new ArrayList<String>();
	private List<List<String>> child=new ArrayList<List<String>>();
	private ExpandableAdapter adapter;
	private ExpandableListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initData();
		initView();
	}

	private void initView() {
		listView=(ExpandableListView) findViewById(R.id.exlistview);
		adapter=new ExpandableAdapter();
		listView.setAdapter(adapter);
		listView.setCacheColorHint(0); //防止拖动时出现黑色背景
		listView.setGroupIndicator(null);//设置自带的箭头消失
		listView.setDivider(null);//设置没有分割线
		
		listView.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Toast.makeText(MainActivity.this, "点击了"+adapter.getChild(groupPosition, childPosition), 1000).show();
				return false;
			}
		});
	}

	private void initData() {
		//添加父布局数据
		group.add("测试数据1");
		group.add("测试数据2");
		group.add("测试数据3");
		group.add("测试数据4");
		group.add("测试数据5");
		//添加子布局数据
		for(int j=0;j<5;j++){
			List<String> childitem=new ArrayList<String>();
			for(int i=0;i<5;i++){
				childitem.add("子数据"+i);
			}
			child.add(childitem);
		}
	}
	class ExpandableAdapter extends BaseExpandableListAdapter{

		@Override
		public int getGroupCount() {
			return group.size();//获取父布局个数
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return child.get(groupPosition).size();//获取子布局个数
		}

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

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return child.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) {
			if(convertView==null){
				LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView=inflater.inflate(R.layout.listviewitem, null);
			}
			TextView tvparent=(TextView) convertView.findViewById(R.id.tvparent);
			tvparent.setText(getGroup(groupPosition).toString());
			ImageView ivView=(ImageView) convertView.findViewById(R.id.ivfx);
			if(isExpanded){
				ivView.setBackgroundResource(R.drawable.btn_browser2);
			}else{
				ivView.setBackgroundResource(R.drawable.btn_browser);
			}
			return convertView;
		}
		//子布局样式
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if(convertView==null){
				LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView=inflater.inflate(R.layout.listviewchild, null);
			}
			TextView tvchild=(TextView) convertView.findViewById(R.id.tvchild);
			tvchild.setText(getChild(groupPosition, childPosition).toString());
			return convertView;
		}

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


点击下载Demo



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章