2010-06-24 16 views

cevap

7

Bu iki gün önce karşılaşıyorum. İşte benim çözüm:

İlk ExpandableListActivity genişletmek ve bunun gibi onCreate() yöntemini geçersiz:

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

BaseExpanableListAdapterExample tanımı (ExpandableExample iç sınıfı) olduğu: Sonra

protected class BaseExpandableListAdapterExample extends BaseExpandableListAdapter { 
} 

İhtiyacınız getChildView ve getGroupView'u uygulamak. Yaptığım şey:

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

Sonra /res/layout/ dizin altında hem group_row.xml ve child_row.xml tanımlamanız gerekir. aşağıdaki gibi Örneğin, benim group_row.xml geçerli:
<?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> 

yardımcı olur Umut. Herhangi bir sorunuz varsa yorum yapın.

+0

Çok teşekkürler. Bu harika ama XML'den de içerik almam gerekiyor. Belki de XML'imi SimpleExpandableListAdapter'da kullanıma uygun veri yapılarına elle ayrıştırmayı düşünmeliyim? –

+0

Aleksander, http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html adresinde, bir ExpandableListView öğesinin manüel olarak nasıl kullanılacağı hakkında Adaptör. – licorna

+0

Bağlantının doğru olduğundan emin misiniz? Bu kod bir sağlayıcı kullanıyor. Sadece kendi birini tanımlar :) Ancak XML verilerini ayrıştıracak kendi sağlayıcıyı oluşturmak bir çözüm gibi görünüyor. –