最近项目中用到了 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