2015-04-15 8 views
7

पर सूची की सूची को कैसे मैप करें, मैं एंड्रॉइड में रीसाइक्लर व्यू में List<List<Object>> मैप करने का प्रयास कर रहा हूं, लेकिन परिणाम पूरी तरह से गड़बड़ हो गया है।
उदाहरण के लिए, मैं इस तरह की सूची की एक सूची है (बस समझा, नहीं मेरा असली मामले के लिए):RecyclerView

List<List<String>> list = {{a, b, c}, {d, e}, {f, g, h, i}}; 

Recyclerview (पहली पंक्ति की तरह प्रदर्शित करना चाहिए तीन subrows होता है, दूसरा दो है और पिछले चार है):

|----a-----| 
|----b-----| 
|----c-----| 
|=======| 
|----d-----| 
|----e-----| 
|=======| 
|----f-----| 
|----g-----| 
|----h-----| 
|----i-----| 

लेकिन परिणाम बिल्कुल उपरोक्त आदेश की तरह नहीं है। कुछ तत्व डुप्लिकेट किया गया है और कुछ गायब हो जाते हैं। उदाहरण के लिए:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    List<Event> list = eventList.get(position); 
    if (holder.rows < list.size()) { 
     holder.rowViewGroup.removeAllViews(); 
     holder.rows = 0; 
     ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     TextView startView = new TextView(context); 
     startView.setLayoutParams(layoutParams); 
     Event headEvent = list.get(0); 
     Calendar startCal = headEvent.getStartCal(); 
     startView.setText(String.format("%tD", startCal)); 
     holder.rowViewGroup.addView(startView); 

    for (final Event event : list) { 
     View element = LayoutInflater.from(context).inflate(R.layout.element, null); 
     //start time view 
     TextView startTimeView = (TextView)element.findViewById(R.id.eventStartTimeInElement); 
     Calendar startTimeCal = event.getStartCal(); 
     startTimeView.setText(String.format("%tl:%tM %tp", startTimeCal, startTimeCal, startTimeCal)); 
     //end date time view 
     TextView endView = (TextView)element.findViewById(R.id.eventEndTimeInElement); 
     Calendar endCal = event.getEndCal(); 
     endView.setText(String.format("%tD %tl:%tM %tp", endCal, endCal, endCal, endCal)); 
     //title view 
     TextView title = (TextView)element.findViewById(R.id.eventTitleInElement); 
     title.setText(event.getTitle()); 
     //member name 

     Drawable divider = context.getResources().getDrawable(R.drawable.divider); 
     ImageView dividerView = new ImageView(context); 
     dividerView.setImageDrawable(divider); 
     holder.rowViewGroup.addView(dividerView); 
     holder.rowViewGroup.addView(element); 
     holder.rows++; 
     } 
    } 
} 

यहाँ मेरी row.xml है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/recycleViewRow"> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cardView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     card_view:cardCornerRadius="0dp" 
     card_view:cardElevation="2sp" 
     card_view:cardUseCompatPadding="true" 
     > 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/rowView" 
      android:paddingLeft="20dp" 
      android:paddingRight="20dp"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/eventStartTimeInRow" 
       /> 

      </LinearLayout> 

    </android.support.v7.widget.CardView> 

और element.xml (पंक्ति के अंतर्गत होता है जो

|----d-----| 
|----e-----| 
|----c-----| 
|=======| 
|----d-----| 
|----e-----| 
|=======| 
|----f-----| 
|----a-----| 

यहाँ मेरी कोड का एक टुकड़ा है। एक्सएमएल):

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



    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Small Text" 
      android:id="@+id/eventStartTimeInElement" 
      android:layout_weight="2" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Small Text" 
      android:id="@+id/eventEndTimeInElement" 
      android:gravity="end" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="left"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"        
      android:textAppearance="?android:attr/textAppearanceMedium"        
      android:text="Medium Text" 
      android:id="@+id/eventTitleInElement"/> 

      </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right"> 

      <android.support.v7.widget.CardView 
       xmlns:card_view="http://schemas.android.com/apk/res-auto" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       card_view:cardCornerRadius="2dp" 
       card_view:cardElevation="2sp" 
       card_view:cardUseCompatPadding="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="Small Text" 
       android:id="@+id/memberNameInElement" 
       android:gravity="end" 
       /> 
      </android.support.v7.widget.CardView> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

</LinearLayout> 

और मुझे पता है कि यह वास्तव में नहीं है इसे लागू करने के लिए ओडी विचार, तो क्या कोई मुझे इसे लागू करने के लिए एक बेहतर तरीका बता सकता है? धन्यवाद ...

उत्तर

0

मुझे लगता है कि आपकी सबसे अच्छी शर्त सूचियों की सूची को एक सूची में फ़्लैट करना है।

यह सिर्फ ListView के प्रयोजनों के लिए है। आपका एडाप्टर कार्य करेगा, हालांकि सूचियों को सभी एक लंबी सूची में घुमाया गया है।

अपने नेस्ट सूचियों के सभी मदों के लिए एक एकल सूची सूचकांक बनाकर शुरुआत करें।

उदाहरण के लिए:

List<List<String>> listOfLists = ... // your original data 
    List<String> listForView = new ArrayList<String>(); 

    for (List<String> innerList : listOfLists) { 
     for (String str : innerList) { 
      listForView.add(str); 
     } 
     listForView.add("---"); // e.g. marker value for divider 
    } 
अब में अपने एडाप्टर के getItem() केवल listForView.get(position) लौटने की जरूरत

कभी भी अपने नेस्टेड सूची संरचना में परिवर्तन, तो आप इस सपाट आपरेशन फिर से करना और notifyDataSetChanged() कहते हैं।

चीजें पाने के एक छोटे से जटिल काम करता है, तो स्थिति को देखते हुए — आप यह पता लगाने की जो सूची एक आइटम में है, लेकिन आप हमेशा सूचकांक सकता है — इसी तरह से भीतरी सूचियों।

+0

लेकिन मैं वास्तव में प्रत्येक समूह को अलग करने के लिए कार्डव्यू का उपयोग करना चाहता हूं। मैं इसी तरह कुछ करने के लिए देख रहा हूँ - क्या होगा यदि मैं एक समतल सूची – CrackThisRay

+0

xia0guang का उपयोग कर इस लक्ष्य को हासिल करने के लिए एक बेहतर तरीका है। क्या आपने कोई प्रगति की है? –

+0

एआई एल - मैं इसे हल करने के लिए विभिन्न प्रकार की पंक्ति का उपयोग करता हूं। तुम्हें कोशिश करनी चाहिए। बस getItemType() को ओवरराइड और अलग पंक्तियों का एक समूह बनाते हैं। मेरे मामले के लिए, मैं sublist के आकार का उपयोग itemtype से संकेत मिलता है और उप पंक्ति के विभिन्न आकार को जोड़ने के लिए पाश के लिए उपयोग करने के लिए। शायद आपकी मदद करेगा। – CrackThisRay