2010-09-16 8 views
13

क्या एंड्रॉइड के addHeaderView() का उपयोग एक ही सूची दृश्य में एकाधिक शीर्षलेख जोड़ने के लिए किया जा सकता है? क्या कोई ऐसा उदाहरण दे सकता है कि यह कैसे करें?addHeaderView() के साथ एक एकल सूची दृश्य में एकाधिक शीर्षलेख जोड़ने के लिए कैसे?

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

class IconicAdapter extends ArrayAdapter<String> { 
    IconicAdapter() { 
     super(ContactTabProfileResource.this, R.layout.row_iconic, mArrayList); 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = getLayoutInflater(); 
     View row = null; 

     if(position == 1 || position == 5) { // phone 
      row = inflater.inflate(R.layout.row_iconic, parent, false); 
      TextView label =(TextView)row.findViewById(R.id.label); 
      label.setText(mArrayList.get(position)); 
      ImageView icon = (ImageView)row.findViewById(R.id.rowicon); 
      icon.setImageResource(R.drawable.icon_phone); 
     } else if (position == 2 || position == 6) { // email 
      row = inflater.inflate(R.layout.row_iconic, parent, false); 
      TextView label =(TextView)row.findViewById(R.id.label); 
      label.setText(mArrayList.get(position)); 
      ImageView icon = (ImageView)row.findViewById(R.id.rowicon); 
      icon.setImageResource(R.drawable.icon_email); 
     } else if (position == 0 || position == 4) { // section header 
      row = inflater.inflate(R.layout.row_header, parent, false); 
      TextView label =(TextView)row.findViewById(R.id.label); 
      label.setText(mArrayList.get(position)); 
      label.setBackgroundColor(Color.GRAY); 
     } else if (position == 3) { // section divider 
      row = inflater.inflate(R.layout.row_header, parent, false); 
      TextView label =(TextView)row.findViewById(R.id.label); 
      label.setText(" "); 
     } 

     return(row); 

    } 
} 

फिर मैंने दो अलग-अलग एक्सएमएल लेआउट बनाए। row_header.xml हेडर पंक्तियों के लिए है और row_iconic.xml गैर-शीर्ष पंक्तियों के लिए है, जिसमें एक आइकन होता है।

row_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="right" 
>  

    <TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:paddingRight="10dp" 
    android:paddingLeft="10px" 
    android:gravity="left" 
    android:textStyle="bold" 
    />  
</LinearLayout> 

row_iconic.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="right" 
>  

    <TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="16sp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:paddingRight="10dp" 
    android:paddingLeft="44px" 
    /> 
    <ImageView 
    android:id="@+id/rowicon" 
    android:layout_width="40dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp" 
    android:layout_height="30dp"   
    android:src="@drawable/icon" 
    />  
</LinearLayout> 

उत्तर

13

आप क्या चाहते हैं एंड्रॉइड डेवलपर्स एक सूची विभाजक या उपशीर्षक ("शीर्षलेख" और "पाद लेख" केवल सूची के शीर्ष या नीचे रहते हैं) को कॉल कर सकते हैं। आप यह कैसे कर सकते हैं इसका सारांश एक सूची एडाप्टर का उपयोग करके है जो अन्य ListAdapters को लपेटता है और कुछ पंक्तियों के लिए हेडर व्यू प्रकार को वापस करने के लिए पर्याप्त स्मार्ट है और ऑफसेट का ट्रैक रखता है, या अपने स्वयं के मिनी एडाप्टर में उन विभाजक दृश्यों को लपेटता है।

मार्क मर्फी SectionedListAdapter, जीपीएल, जो पहली दृष्टिकोण (जेफ शार्की द्वारा कोड के आधार पर) लेता है, या उसके MergeAdapter पर एक नजर डालें, और this SO question देखते हैं।

यह आईफोन पर स्मार्ट सूची उपशीर्षक की सुंदर संचालन से बहुत रोना है, लेकिन एडाप्टर के अंदर क्या हो रहा है, उसके आस-पास क्या हो रहा है, इसके बाद यह मर्ज एडाप्टर का उपयोग करने के लिए काफी सरल है।

1

आपके पास कॉल addHeaderView() कई बार द्वारा की तरह आप के रूप में कई हेडर जोड़ सकते हैं। एडाप्टर को सूची दृश्य में सेट करने से पहले आपको इसे करना होगा।

+0

मैं एकाधिक शीर्षलेख दृश्य जोड़ने में सक्षम हूं ... लेकिन वे सभी ListView के शीर्ष पर दिखाई दे रहे हैं। मैं ListView के भीतर अनुभाग बनाने के लिए हेडर का उपयोग करना चाहता हूं। यह सुनिश्चित नहीं है कि हेडर्स ListView के शीर्ष के अलावा कहीं भी कैसे दिखें – Chris

7

यदि आप विभिन्न लेआउट (जैसे आइटम & हेडर) वापस करना चाहते हैं तो आपको getItemViewType(int position) का उपयोग करना होगा। तो अपने अनुकूलक कोड इस तरह दिखना चाहिए:

private static final int TYPE_HEADER = 0; 
private static final int TYPE_ICONIC = 1; 

@Override 
public int getViewTypeCount() { 
    return 2; // we have two types, so just return 2 
} 

@Override 
public int getItemViewType(int position) { 
    // TODO: return TYPE_HEADER here if this position is a header, otherwise return TYPE_ICONIC 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO: return the appropriate layout 
    // hint: you might call getItemViewType(position) yourself here to know of which type the layout is 
} 

मूल रूप से है कि यह कैसे काम करता है। यह सुनिश्चित करने के लिए कि हेडर चुनने योग्य नहीं हैं, आप अपने हेडर दृश्यों के लिए झूठी वापसी के लिए ArrayAdapter को फेंकना और इसके बजाय ListAdapter का उपयोग करना चाहेंगे, isEnabled(int position) ओवरराइड करना। ;-)

0

आप उदाहरण के रूप में this class का भी उपयोग कर सकते हैं। यह "सेक्शन" और "एक्शन" का समर्थन करता है, जहां "सेक्शन" केवल "क्रियाएं" (आइटम) के समूह हैं, उनके पास गैर-क्लिक करने योग्य और गैर-चयन योग्य आइटम - हेडर है। "क्रियाएं" onClick हैंडलर निर्दिष्ट कर सकती हैं जिसे तब "कॉल" दबाया जाएगा, यदि मौजूद हो। इसमें वेब से "क्रियाएं" के लिए छवियां लाने के लिए ImageLoader भी शामिल है।

संबंधित मुद्दे