2010-06-24 14 views

Odpowiedz

7

biegnę do tego dwa dni temu. Oto moje rozwiązanie:

Pierwszy przedłużyć ExpandableListActivity i zastąpić metodę onCreate() tak:

public class ExpandableExample extends ExpandableListActivity { 
    @Override 
    public void onCreate() { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new BaseExpandableListAdapterExample()); 
    } 
} 

Definicja BaseExpanableListAdapterExample jest (wewnętrzna klasa ExpandableExample):

protected class BaseExpandableListAdapterExample extends BaseExpandableListAdapter { 
} 

to trzeba do implementacji getChildView i getGroupView. Co zrobiłem było:

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; 
} 

Następnie trzeba zdefiniować zarówno group_row.xml i child_row.xml pod /res/layout/ katalogu. Na przykład moja group_row.xml jest następująca:

<?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> 

Mam nadzieję, że pomoże. Skomentuj, jeśli masz jakieś pytanie.

+0

Dziękuję bardzo. To świetnie, ale muszę również pobrać zawartość z XML. Może powinienem rozważyć ręczne przetwarzanie mojego XML do struktur danych odpowiednich do użycia w SimpleExpandableListAdapter? –

+0

Aleksander, spójrz na http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html, aby zobaczyć przykład ręcznego wypełniania widoku ExpandableListView bez użycia Adapter. – licorna

+0

Czy jesteś pewien, że link jest poprawny? Ten kod używa dostawcy. Po prostu definiuje swój własny :) Ale stworzenie własnego dostawcy, który będzie parsował dane XML, wydaje się być rozwiązaniem. –

Powiązane problemy