Android xml中的ExpandableListView定义
我需要在xml文件中手动定义ExpandableListView的结构。 有这样做的指南或例子吗Android xml中的ExpandableListView定义,android,Android,我需要在xml文件中手动定义ExpandableListView的结构。 有这样做的指南或例子吗 谢谢我两天前碰到这个。以下是我的解决方案: 首先扩展ExpandableListActivity,并像这样重写onCreate()方法: public class ExpandableExample extends ExpandableListActivity { @Override public void onCreate() { super.onCreate(sa
谢谢我两天前碰到这个。以下是我的解决方案: 首先扩展
ExpandableListActivity
,并像这样重写onCreate()方法:
public class ExpandableExample extends ExpandableListActivity {
@Override
public void onCreate() {
super.onCreate(savedInstanceState);
setListAdapter(new BaseExpandableListAdapterExample());
}
}
BaseExpanableListAdapterExample
的定义是(可扩展示例的内部类):
然后需要实现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;
}
然后您需要在/res/layout/
目录下定义组行.xml
和子行.xml
。例如,我的group_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>
希望能有帮助。如果您有任何问题,请发表评论。非常感谢。这很好,但我也需要从XML检索内容。也许我应该考虑将XML手动解析成适合在SimeExpDelabelistAdvor Aleksander中使用的数据结构,请看一个关于如何在不使用适配器的情况下手动填充ExpDabeleistVIEW的示例。您确定链接是否正确?该代码确实使用了提供程序。它只是定义了自己的一个:),但创建自己的提供者来解析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>