I run into this two days ago. 我的解决方案是:
第一次推广
ExpandableListActivity
并重写onCreate()方法,如下所示:
public class ExpandableExample extends ExpandableListActivity {
@Override
public void onCreate() {
super.onCreate(savedInstanceState);
setListAdapter(new BaseExpandableListAdapterExample());
}
}
定义
BaseExpanableListAdapterExample
是(内部类
ExpandableExample
):
protected class BaseExpandableListAdapterExample extends BaseExpandableListAdapter {
}
然后你需要实施
getChildView
和
getGroupView
. 我所做的是:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View groupRow = getLayoutInflater().inflate(R.layout.group_row, null);
TextView textView = (TextView) groupRow.findViewById(R.id.text_group);
textView.setText(getGroup(groupPosition).toString());
return groupRow;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View childRow = getLayoutInflater().inflate(R.layout.child_row, null);
TextView textView = (TextView) childRow.findViewById(R.id.text_child);
textView.setText(getChild(groupPosition, childPosition).toString());
return childRow;
}
然后你需要定义两者
group_row.xml
和
child_row.xml
在下面
/res/layout/
目录。例如,我的
GROMP.ROW.XML
如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text_element"
android:layout_width="fill_parent"
android:layout_height="48dip"
android:textSize="20sp"
android:textColor="#fff"
android:layout_marginLeft="48dip"
android:gravity="center_vertical" />
</RelativeLayout>
希望它有帮助。如果您有任何问题,请发表评论。