2010-06-24 4 views
6

में विस्तारणीय लिस्ट व्यू परिभाषा मुझे एक्सएमएल फ़ाइल में एक्सपेंडेबल लिस्ट व्यू की संरचना को मैन्युअल रूप से परिभाषित करने की आवश्यकता है। क्या ऐसा करने का कोई गाइड या उदाहरण है?एक्सएमएल

धन्यवाद

उत्तर

7

मैं इस दो दिन पहले भागता हूं। यहाँ मेरी समाधान है:

पहले 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; 
} 

तो फिर तुम /res/layout/ निर्देशिका के अंतर्गत दोनों group_row.xml और child_row.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> 

आशा है कि यह मदद करता है। अगर आपके पास कोई सवाल है तो टिप्पणी करें।

+0

बहुत बहुत धन्यवाद। यह बहुत अच्छा है लेकिन मुझे XML से भी सामग्री पुनर्प्राप्त करने की आवश्यकता है। हो सकता है कि मुझे SimpleExpandableListAdapter में उपयोग करने के लिए उपयुक्त डेटा संरचनाओं के लिए मैन्युअल रूप से मेरे एक्सएमएल को पार्स करना चाहिए? –

+0

Aleksander, http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html पर एक उदाहरण के लिए मैन्युअल रूप से एक विस्तारणीय लिस्ट व्यू को पॉप्युलेट करने के तरीके पर एक उदाहरण के लिए देखें, बिना किसी प्रयोग किए एडाप्टर। – licorna

+0

क्या आप वाकई सही हैं? वह कोड प्रदाता का उपयोग करता है। यह बस इसे अपने आप को परिभाषित करता है :) लेकिन खुद का प्रदाता बनाना जो एक्सएमएल डेटा को पार्स करेगा, एक समाधान की तरह दिखता है। –