布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
主程序代码:
package com.toby.personal.testlistview;
import android.app.Activity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter adapter = new ExpandableListAdapter() {
private final int[] types = new int[]{
R.drawable.img01, R.drawable.dog_001, R.drawable.girl01
};
private final String[] typeNames = new String[]{
"img", "dog", "girl"
};
private final String[][] items = new String[][]{
{"img01", "img02", "img03", "img04", "img05", "img06", "img07", "img08"},
{"dog01", "dog02", "dog03", "dog04", "dog05"},
{"girl01", "girl02", "girl03", "girl04", "girl05"}
};
private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
return textView;
}
@Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
}
@Override
public int getGroupCount() {
return typeNames.length;
}
@Override
public int getChildrenCount(int groupPos) {
return items[groupPos].length;
}
@Override
public Object getGroup(int groupPos) {
return typeNames[groupPos];
}
@Override
public Object getChild(int groupPos, int childPos) {
return items[groupPos][childPos];
}
@Override
public long getGroupId(int groupPos) {
return groupPos;
}
@Override
public long getChildId(int groupPos, int childPos) {
return childPos;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView,
ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView type = new ImageView(MainActivity.this);
type.setScaleType(ImageView.ScaleType.FIT_XY);
type.setLayoutParams(new LinearLayout.LayoutParams(300, 300));
type.setImageResource(types[groupPos]);
ll.addView(type);
TextView textView = getTextView();
textView.setText(getGroup(groupPos).toString());
ll.addView(textView);
return ll;
}
@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPos, childPos).toString());
return textView;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int i) {
}
@Override
public void onGroupCollapsed(int i) {
}
@Override
public long getCombinedChildId(long l, long l1) {
return 0;
}
@Override
public long getCombinedGroupId(long l) {
return 0;
}
};
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.listView);
expandableListView.setAdapter(adapter);
}
}
运行效果图:
参考文献:《疯狂Android讲义(第2版)》