package com.crazyit.ui.adapterview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 可展开的列表组件 ExpandableListview
*/
public class ExpandableListviewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_listview);
//创建一个 ExpandableListAdapter对象
ExpandableListAdapter expandableListAdapter = new BaseExpandableListAdapter() {
int[] icons = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.libai};
private String[] armTypes = new String[]{"魏国", "蜀国", "吴国", "晋朝"};
private String[][] arms = new String[][]{
{"曹操", "曹丕", "曹植"},
{"刘备", "孔明", "赵云"},
{"周瑜", "鲁肃", "陆逊"},
{"司马懿", "司马昭", "司马炎"}};
//获取指定组位置 指定子列表处的子列表数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].length;
}
private TextView getTextview() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textview = new TextView(ExpandableListviewActivity.this);
textview.setLayoutParams(lp);
textview.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textview.setPadding(70, 0, 0, 0); //在此可以设置 图片和文字之间的间隔距离
textview.setTextSize(24);
return textview;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextview();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组的 组数据
@Override
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
}
@Override
public int getGroupCount() {
return armTypes.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(ExpandableListviewActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView logo = new ImageView(ExpandableListviewActivity.this);
logo.setImageResource(icons[groupPosition]);
ll.addView(logo);
TextView tv = new TextView(getApplicationContext());
tv.setText(getGroup(groupPosition).toString());
ll.addView(tv);
return ll;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
};
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setAdapter(expandableListAdapter);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.crazyit.ui.adapterview.ExpandableListviewActivity">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:childIndicator="@drawable/nongyu"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
></ExpandableListView>
<!--显示在子列表旁边的Drawable对象 android:childIndicator-->
</LinearLayout>
上面程序的关键代码就是扩展 BaseExpandableListAdapter来实现ExpandableListAdapter . 当使用扩展 BaseExpandableListAdapter时,关键是实现如下4个方法.
- getGroupCount( );该方法返回包含组列表项的数量.
- getGroupView( ); 该方法返回View对象作为组列表项.
- getChildrenCount( );该方法返回特定组所包含的子列表项的数量.
- getChildView( ); 该方法返回的View对象作为特定组的,特定位置的子列表项.决定每个子选项的外观
上面的代码中 getChildView( )方法中,返回了一个普通的文本(TextView) 因每一个子列表项是都是一个普通文本框, getGroupView( )方法 返回了LinearLayout对象,该LinearLayout包含了一个 ImageView和一个Textview ,因此每个列表项都是由图片和文本组成.