Android ExpandableListView 使用要点笔记

最近项目中用到了 ExpandableListView,对其用法深入了解了一下,在此 Mark一下!




展开某一个group item后




package com.example.elvtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class MainActivity extends Activity {

	private MyExpandableListAdapter myExpandableListAdapter;
	private ExpandableListView mELV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findView();
	}

	private void findView() {
		
		mELV = (ExpandableListView) findViewById(R.id.mELV);
		
		//给 ExpandableListView 添加一个头部,类似奇艺首页 UI效果
//		mELV.addHeaderView(v);
		
		myExpandableListAdapter = new MyExpandableListAdapter();
		mELV.setAdapter(myExpandableListAdapter);
		
//		mELV.collapseGroup(groupPos);	//关闭某个父级group
//		mELV.expandGroup(groupPos);		//展开某个父级group
		
		//设置列表只展开一项
		mELV.setOnGroupExpandListener(new OnGroupExpandListener() {
			
			@Override
			public void onGroupExpand(int groupPosition) {
				//展开某一项的同时,关闭其他所有的 group item
				for (int i = 0; i < myExpandableListAdapter.getGroupCount(); i++) {
					if (groupPosition != i) {
						mELV.collapseGroup(i);
					}
				}
				
				mELV.setSelectedGroup(groupPosition);	  //设置被选中的group置于顶端   
			}
		});
		
//		mELV.setOnGroupClickListener(onGroupClickListener);
//		mELV.setOnGroupCollapseListener(onGroupCollapseListener);	//设置 ExpandableListView 列表关闭事件监听
//		mELV.setOnGroupExpandListener(onGroupExpandListener);		//设置 ExpandableListView 列表展开事件监听
	}
	
	private class MyExpandableListAdapter extends BaseExpandableListAdapter{

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return null;
		}

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

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

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

		//group item 布局
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return null;
		}

		//child item 布局,可以是GridView或其他任意布局
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return null;
		}

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

}



<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=".MainActivity" >

    <ExpandableListView
        android:id="@+id/mELV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:cacheColorHint="@android:color/transparent"
        android:childDivider="@color/white"
        android:divider="@color/white"
        android:dividerHeight="2dp"
        android:groupIndicator="@null"
        android:listSelector="@android:color/transparent"
        android:scrollbars="none" >
    </ExpandableListView>

</RelativeLayout>



如果想在 child item 中做展开、关闭的动画,可以参考github 上的 ListViewAnimations

实现效果很炫,链接地址:https://github.com/nhaarman/ListViewAnimations




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值