ExpandableListView是对ListView的扩展,实现了当点击列表项时弹出下一层的列表项
同时它必须要通过相对应的适配器来实现数据的绑定
1、首先在布局文件中创建好组件
<ExpandableListView
android:id="@+id/extendview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/myspinner"
>
</ExpandableListView>
第一层
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/groupimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/grouptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/groupimage"
/>
</RelativeLayout>
第二层
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/childimage"
android:layout_width="40dp"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/childtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/childimage"
/>
</RelativeLayout>
界面创建好之后,接下来通过java代码来实现数据绑定
2、数据绑定
自定义一个组件组类,用来封装需要显示在ExpandableListView中的组件
//自定义组件类
public class MyView{
private ImageView imageView;
private TextView textView;
}
定义一个方法用来封装向ExpandableListView的过程
//BaseExpandableListAdapter适配器向ExpandableListView填充数据,实现点击弹出下拉
public void extendListView(){
//定义第一层的数据
final String[] textarr={"图片1","图片2","图片3","图片4"};//需要在textView中显示的数据
final int[] imgarr={R.drawable.iphone_we_flash,R.drawable.iphone_we_front,R.drawable.leica_active,R.drawable.leica_off};//需要显示的图片
//定义第二层数据
final String[][] childtextarr={{"图片1","图片2","图片3","图片4"},{"图片1","图片2","图片3","图片4"},{"图片1","图片2","图片3","图片4"},{"图片1","图片2","图片3","图片4"}};
final int[][] childimgarr={{R.drawable.iphone_we_flash,R.drawable.iphone_we_front,R.drawable.leica_active,R.drawable.leica_off},
{R.drawable.iphone_we_flash,R.drawable.iphone_we_front,R.drawable.leica_active,R.drawable.leica_off},
{R.drawable.iphone_we_flash,R.drawable.iphone_we_front,R.drawable.leica_active,R.drawable.leica_off},
{R.drawable.iphone_we_flash,R.drawable.iphone_we_front,R.drawable.leica_active,R.drawable.leica_off}
};
//以匿名内部类的方式来获得BaseExpandableListAdapter适配器的对象
BaseExpandableListAdapter bAdapter=new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
//返回一个第一层的组件
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//创建一个自定义组件容器对象
MyView myView=null;
if(convertView==null){
//将组件的XML布局文件转换为View 组件
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.group_view, null);
myView=new MyView();
myView.imageView=(ImageView)convertView.findViewById(R.id.groupimage);//获得转换得View的组件
myView.textView=(TextView)convertView.findViewById(R.id.grouptext);
convertView.setTag(myView);//将封装成对象的自定义组件对象设置为convertView的Tag
}else{
myView=(MyView) convertView.getTag();
}
myView.imageView.setImageResource(imgarr[groupPosition]);
myView.textView.setText(textarr[groupPosition]);
return convertView;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return imgarr.length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childimgarr[groupPosition].length;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
MyView myView=null;
if(convertView==null){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.child_view, null);
myView=new MyView();
myView.imageView=(ImageView)convertView.findViewById(R.id.childimage);
myView.textView=(TextView)convertView.findViewById(R.id.childtext);
convertView.setTag(myView);
}else{
myView=(MyView) convertView.getTag();
}
myView.imageView.setImageResource(childimgarr[groupPosition][childPosition]);
myView.textView.setText(childtextarr[groupPosition][childPosition]);
return convertView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
};
ExpandableListView e=(ExpandableListView)findViewById(R.id.extendview);
e.setAdapter(bAdapter);
}
至此已实现向ExpandableListView中填充数据