ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像手机QQ中的好友列表就是实现的类似效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类。
MainActivity.java
代码语言:javascript复制package com.example.expandablelistview;
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.TextView;
public class MainActivity extends Activity {
private ExpandableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ExpandableListView)findViewById(R.id.expandableListView1);
listView.setAdapter(new MyExpandableAdapter());
}
class MyExpandableAdapter extends BaseExpandableListAdapter{
private String[] groups = {"好友", "扣丁学堂"};
private String[][] childs = {{"猪哥", "蛋蛋"}, {"威哥", "阿珂"}};
@Override
public boolean areAllItemsEnabled() { return false; }
@Override
public Object getChild(int index1, int index2) {
return childs[index1][index2];
}
@Override
public long getChildId(int index1, int index2) {
return index2;
}
@Override
public View getChildView(int index1, int index2, boolean arg2,
View view, ViewGroup parent) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.child, parent, false);
}
TextView tv = (TextView)view.findViewById(R.id.title);
tv.setText(childs[index1][index2]);
return view;
}
@Override
public int getChildrenCount(int index) {
return childs[index].length;
}
@Override
public long getCombinedChildId(long arg0, long arg1) { return 0; }
@Override
public long getCombinedGroupId(long arg0) { return 0; }
@Override
public Object getGroup(int index) {
return groups[index];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int index) {
return index;
}
@Override
public View getGroupView(int index, boolean arg1, View view,
ViewGroup parent) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.group, parent, false);
}
TextView tv = (TextView)view.findViewById(R.id.title);
tv.setText(groups[index]);
return view;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
}
main.xml
代码语言:javascript复制<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
tools:context="com.example.expandablelistview.MainActivity" >
<ExpandableListView
android:id="@ id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
group.xml
代码语言:javascript复制<?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="horizontal"
android:gravity="left"
tools:context=".MainActivity" >
<ImageView
android:contentDescription="@null"
android:id="@ id/icon"
android:maxWidth="60sp"
android:maxHeight="60sp"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@ id/title"
android:textSize="40sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
child.xml
代码语言:javascript复制<?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="horizontal"
android:gravity="left"
android:paddingLeft="30sp"
tools:context=".MainActivity" >
<ImageView
android:contentDescription="@null"
android:id="@ id/icon"
android:maxWidth="60sp"
android:maxHeight="60sp"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@ id/title"
android:textSize="40sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
效果图: