2015-04-15 94 views
7

我試圖將List<List<Object>>映射到Android中的RecyclerView,但結果完全搞砸了。
例如,我有這樣的目錄列表(只是爲了解釋,不是我的真實案例):如何將列表映射到RecyclerView

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

的Recyclerview應該顯示像(第一行包含三個子行,第二個有兩個和最後一個上有四個):

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

但結果與上述順序不完全相同。一些元素被複制並且一些消失。例如:

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

這裏是一塊我的代碼:

@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(這是由行含有。 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> 

而且我知道這不是真的去想法實施這個,所以任何人都可以告訴我一個更好的方法來實現這個?謝謝...

回答

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

但我真的想用CardView來分隔每個組。如果我使用扁平列表,則實現此目的的更好方法是 – CrackThisRay

+0

xia0guang - 我正在尋找類似的東西。你有沒有進展? –

+0

AI L--我使用不同類型的行來解決它。你應該試試。只需重寫getItemType()並製作一堆不同的行。對我而言,我使用子列表的大小來指示itemType並使用for循環來添加不同大小的子行。也許會幫助你。 – CrackThisRay