2015-04-22 181 views
0

我正在創建一個包含不同類型視圖的列表。 像Facebook一樣在移動應用程序中顯示其供稿示例某些時候滾動視圖或某些時間列表內部列表。recyclerview項目中的不同適配器

要做到這一點將是一個不錯的選擇。 如果我在回收站的每個項目中添加一個片段,該怎麼辦?像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 

<fragment 
    android:layout_width="match_parent" 
    android:name="com.profile.EditProfileFragment" 
    android:layout_height="wrap_content"/> 

,並添加本作中Reclycleview一個項目,並保持其內部不同的邏輯。

任何人都可以告訴我如何去通過這個。

回答

0

我不認爲這將是可能的使用片段作爲一個行。片段有其自己的生命週期,我不在於RecyclerView可以控制的內容。 但你可以用簡單的視圖來做到這一點。

只是覆蓋一些方法RecyclerView.Adapter

private ArrayList<Data> items = new ArrayList<>(); // data associated 
// with each row 

@Override 
public int getItemViewType(int position) { 
    return items.get(position).getRowType(); 
// assuming this is the getter to get the type 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    switch (viewType) { 
     case Data.TYPE_A: 
      View convertView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.row_feed, parent, false); 
      return new ViewHolderTypeA(convertView); 
     case Data.TYPE_B: 
     ... 
     default: 
      return null; 
    } 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    Data data = items.get(position); 

    if (holder instanceof ViewHolderTypeA) { 
     ViewHolderTypeA holderA= (ViewHolderTypeA) holder; 
     // manipulate the views in view holder for this type 
     ... 
    }else if (holder instanceof ViewHolderTypeB){ 

    } 
} 

編輯:你並不需要不同的適配器爲每個項目。你只需要包含所有邏輯的項目。所以,如果你想有兩種類型,一種爲狀態,一個用於照片像Facebook養活那麼你的項目將是:

class Data { 
    String status, photoUrl; 
} 

當然一些成員將根據該行是空的。

+0

我知道這一點,也用於然而,如果我想爲每個項目然後添加一個不同的邏輯。意味着我必須爲每個項目添加一個適配器。 – Bora

+0

我編輯了我的答案。 – inmyth

相關問題