2015-04-15 19 views
7

için nasıl eşleştirilir List<List<Object>>'u Android'deki RecyclerView'a eşleştirmeye çalışıyorum, ancak sonuç tamamen dağınık.
Örneğin, böyle listenin bir listesi var (sadece benim gerçek vaka, açıklayan):Listenin listesi RecyclerView

Recyclerview (ilk satırda gibi göstermesi gerekir
List<List<String>> list = {{a, b, c}, {d, e}, {f, g, h, i}}; 

üç subrows içeren ikinci iki sahiptir ve son dörtten:) yukarıdaki gibi tam olarak aynı değildir. Bazı elemanlar çoğaltılır ve bazıları kaybolur. örneğin: Burada

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

benim row.xml geçerli::

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

ve element.xml (satır tarafından bulunan

İşte
|----d-----| 
|----e-----| 
|----c-----| 
|=======| 
|----d-----| 
|----e-----| 
|=======| 
|----f-----| 
|----a-----| 

benim kod parçasıdır. xml):

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

Ve bunun gerçekten gitmediğini biliyorum Bunu uygulamak için bir fikir, bu yüzden lütfen bunu uygulamak için daha iyi bir yol söyleyebilir misiniz? Teşekkürler ...

cevap

0

Bence en iyi bahsiniz liste listenizi tek bir listeye oturtmaktır.

Bu, yalnızca ListView'un amaçları içindir. Bağdaştırıcınız, listelerin hepsi uzun bir listeye yerleştirilmiş gibi davranacaktır.

İç içe geçmiş listelerinizin tüm öğeleri için tek bir liste dizini oluşturarak başlayın. Örneğin

: En adaptör en getItem() sadece listForView.get(position) dönmek gerek Şimdi

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 
    } 

.

İç içe geçmiş liste yapınızın her değişmesi değişir, bu düzleştirme işlemini yineleyin ve notifyDataSetChanged() numaralı telefonu arayın.

şeyler almak biraz bir öğeyi içinde olan liste anlamaya var ama endeks hep could — pozisyon verilmiş yanıltıcıdır eğer — benzer bir şekilde iç listeleri.

+0

Ancak, her grubu ayırmak için CardView'ı kullanmak istiyorum. Düz bir liste – CrackThisRay

+0

xia0guang kullanıyorsanız bunu elde etmek için daha iyi bir yaklaşım nedir - Ben benzer bir şey yapmak için arıyorum. Bir gelişme kaydettin mi? –

+0

AI L - Bunu çözmek için farklı bir satır türü kullanıyorum. denemelisin. sadece getItemType() öğesini geçersiz kılın ve farklı satırlardan bir demet yapın. Benim durumum için, itemType'ı belirtmek için alt liste boyutunu kullanıyorum ve alt satırın farklı boyutunu eklemek için döngü için kullanıyorum. belki sana yardım eder. – CrackThisRay