expandableListView通常用在继承ExpandableListActivity里面,直接调用getExpandableListView()获得expandableListView控件,若没有调用该函数反而会报错,可以参考:http://blog.csdn.net/cjjky/article/details/6903504
不过通常我们为了兼容多个版本而不继承ExpandableListActivity,用findViewById也可以,不过这都不重要,下面介绍个最简单的demo:其中xml文件里就只有一个ExpandableListView控件
一、简单的expandableListView用法
package com.example.user.spinnertest2;
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.ExpandableListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {//ExpandableListActivity
List<String> group; //组列表
List<List<String>> child; //子列表
// ContactsInfoAdapter adapter; //数据适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expent_listView1);
initializeData();
expandableListView.setAdapter(new MyExpandableAdaptor());
}
class MyExpandableAdaptor 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 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
return getGenericView(group.get(groupPosition));
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
return getGenericView(child.get(groupPosition).get(childPosition));
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//创建组/子视图
public TextView getGenericView(String s) {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 80);
TextView text = new TextView(MainActivity.this);
text.setLayoutParams(lp);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(100,0, 0, 0);
text.setText(s);
return text;
}
}
/**
* 初始化组、子列表数据
*/
private void initializeData(){
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
addInfo("Andy",new String[]{"male","138123***","GuangZhou"});
addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});
addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});
addInfo("Tom",new String[]{"female","138123***","ShangHai"});
addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});
}
/**
* 模拟给组、子列表添加数据
* @param g-group
* @param c-child
*/
private void addInfo(String g,String[] c){
group.add(g);
List<String> childitem = new ArrayList<String>();
for(int i=0;i<c.length;i++){
childitem.add(c[i]);
}
child.add(childitem);
}
}
二、带图片的含自列表的列表用法:
1、和ListView用法类似,也可以用HolderView记录控件免得重新加载,这里就不详细介绍了,demo中用的是未这样优化的简单方式。
2、利用layout.xml文件来显示group和child的视图。
Activity:
package com.example.user.spinnertest2;
import android.content.Context;
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.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {//ExpandableListActivity
List<String> group; //组列表
List<List<String>> child; //子列表
HashMap<String ,Integer> hashmap ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expent_listView1);
initializeData();
hashmap = new HashMap();
getDrawableIdByString();
expandableListView.setAdapter(new MyExpandableAdaptor());
}
class MyExpandableAdaptor 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 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.group , null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_1);
TextView textView = (TextView) view.findViewById(R.id.tv_1);
String text = group.get(groupPosition);
textView.setText(text);
imageView.setImageResource(hashmap.get(text));
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.child , null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_2);
TextView textView = (TextView) view.findViewById(R.id.tv_2);
String text = child.get(groupPosition).get(childPosition);
textView.setText(text);
imageView.setImageResource(hashmap.get(text));
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
/**
* 初始化组、子列表数据
*/
private void initializeData(){
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
addInfo("fruit",new String[]{"apple","banana"});
addInfo("vegetables",new String[]{"eggplant","tomato"});
addInfo("meats",new String[]{"pig","cow"});
}
/**
* 模拟给组、子列表添加数据
* @param g-group
* @param c-child
*/
private void addInfo(String g,String[] c){
group.add(g);
List<String> childitem = new ArrayList<String>();
for(int i=0;i<c.length;i++){
childitem.add(c[i]);
}
child.add(childitem);
}
//将drawable图片的R的引用值存入hashMap中
private void getDrawableIdByString()
{
hashmap.put("fruit" , R.drawable.fruite);
hashmap.put("banana" , R.drawable.banana);
hashmap.put("apple" , R.drawable.apple);
hashmap.put("vegetables" , R.drawable.vegetables);
hashmap.put("eggplant" , R.drawable.eggplant);
hashmap.put("tomato" , R.drawable.tomato);
hashmap.put("meats" , R.drawable.meats);
hashmap.put("pig" , R.drawable.pig);
hashmap.put("cow" , R.drawable.cow);
}
}
gourp.xml:
<?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="horizontal"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/image_1"
android:background="@android:color/darker_gray"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:text="水果"
android:gravity="center_vertical"
/>
</LinearLayout>
child.xml:
<?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">
<ImageView
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/image_2"
android:background="@android:color/darker_gray"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:gravity="center_vertical"
/>
</LinearLayout>
效果图如下: