Android ExpandableListView 使用要点笔记

来源:互联网 发布:淘宝精品摩托车 编辑:程序博客网 时间:2024/06/05 18:23

最近项目中用到了 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;@Overrideprotected 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() {@Overridepublic void onGroupExpand(int groupPosition) {//展开某一项的同时,关闭其他所有的 group itemfor (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{@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn 0;}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn null;}@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn 0;}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn 0;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}//group item 布局@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}//child item 布局,可以是GridView或其他任意布局@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn 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




原创粉丝点击