Ich muss die Struktur von ExpandableListView in XML-Datei manuell definieren. Gibt es eine Anleitung oder ein Beispiel dafür?ExpandableListView Definition in XML
Danke
Ich muss die Struktur von ExpandableListView in XML-Datei manuell definieren. Gibt es eine Anleitung oder ein Beispiel dafür?ExpandableListView Definition in XML
Danke
Ich laufe in diese zwei Tage. Hier ist meine Lösung:
Erste ExpandableListActivity
erweitern und die onCreate() -Methode wie folgt außer Kraft setzen:
public class ExpandableExample extends ExpandableListActivity {
@Override
public void onCreate() {
super.onCreate(savedInstanceState);
setListAdapter(new BaseExpandableListAdapterExample());
}
}
Die Definition von BaseExpanableListAdapterExample
ist (innere Klasse von ExpandableExample
):
protected class BaseExpandableListAdapterExample extends BaseExpandableListAdapter {
}
Dann brauchen Sie zu implementieren getChildView
und getGroupView
. Was ich tat, war:
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;
}
Dann müssen Sie sowohl group_row.xml
und child_row.xml
unter /res/layout/
Verzeichnis definieren. Zum Beispiel ist mein group_row.xml
wie folgt:
<?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>
Ich hoffe, es hilft. Kommentar, wenn Sie Fragen haben.
Vielen Dank. Das ist großartig, aber ich muss auch Inhalte aus XML abrufen. Vielleicht sollte ich meinen XML-Code manuell in Datenstrukturen einbetten, die für die Verwendung in SimpleExpandableListAdapter geeignet sind? –
Aleksander, siehe http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html für ein Beispiel, wie man ein ExpandableListView manuell auffüllt, ohne ein Adapter. – licorna
Sind Sie sicher, dass der Link korrekt ist? Dieser Code verwendet einen Anbieter. Es definiert einfach sein eigenes :) Aber das Erschaffen des eigenen Anbieters, der XML-Daten parsen wird, scheint wie eine Lösung zu schauen. –