2016-04-14 38 views
1

我正在一個社交網絡應用程序中,我需要顯示供稿給訂閱者..用於在列表中顯示供稿。我正在使用RecyclerView。到現在爲止沒有問題...但是當我必須在飼料上顯示一些評論時,問題就出現了。我試圖完成一個類似於Instagram的佈局,他們在主列表中顯示一個圖像並且最多有3-4條評論。我正在從服務器獲取圖像鏈接及其註釋(以JSON數組形式)。有時,我獲得註釋數組大小0和有時1/2 ../ 10。我已經創建了一個包含2 TextView註釋佈局的XML和我在FeedViewHolder基於膨脹是我ViewType ..我的item_comment樣子動態地添加視圖或充氣佈局到RecyclerView的項目

<RelativeLayout 
    android:id="@+id/comment_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="5dp" 
    android:paddingLeft="5dp" 
    android:paddingTop="5dp"> 

    <TextView 
     android:textStyle="bold" 
     android:textColor="@color/indigo_500" 
     android:id="@+id/comment_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:src="@drawable/profile_pic" 
     android:text="Somesh Kumar"/> 

    <TextView 
     android:id="@+id/comment_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/comment_name" 
     android:ellipsize="end" 
     android:paddingLeft="5dp" 
     android:singleLine="true" 
     android:text="This is a comment"/> 
</RelativeLayout> 

我通過我的飼料名單RecyclerView adaptor..and然後我正在檢查該項目(將顯示)是否應該有評論。

@Override 
public int getItemViewType(int position) 
{ 
    FeedParser.DataClass dataClass = feedList.get(position); 
    int ITEM_TYPE = 0; 
    if (dataClass.getPost_type() == FEED_TYPE_PICTURE) // Feed item contains caption with pictures 
    { 
     // check if it has comments 
     if (dataClass.getComments().size() == 0) 
     { 
      ITEM_TYPE = PICTURE_WITHOUT_COMMENTS; 
     } 
     else 
     { 
      ITEM_TYPE = PICTURE_WITH_COMMENTS; 
     } 
    } 
    return ITEM_TYPE; 
} 

並通過ITEM_TYPEonCreateViewHolder

@Override 
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_feed, parent, false); 
    return new FeedViewHolder(itemView, viewType); 
} 

FeedViewHolder充氣項目佈局和評論佈局,如果viewType有評論

 public FeedViewHolder(View itemView, int viewType) 
    { 
     super(itemView); 
     tvFeedUsername = (TextView) itemView.findViewById(R.id.tvFeedUsername); 
     tvFeedCaption = (TextView) itemView.findViewById(R.id.tvFeedCaption); 


     switch (viewType) 
     { 
      // TODO: Write code for other feed type such as picture and video 
      // viewType 1 = PICTURE_WITH_COMMENTS 
      case 1: 
       layoutComments = (LinearLayout) itemView.findViewById(R.id.layoutComments); 
       commentView = layoutInflater.inflate(R.layout.item_comment, null); 
       postCommentText = (TextView) commentView.findViewById(R.id.comment_text); 
       postCommentName = (TextView) commentView.findViewById(R.id.comment_name); 
       layoutComments.addView(commentView); 
       break; 
     } 
    } 

我已經驗證了這個完美的作品,但是當我的setText爲holder.postCommentName & holder.postCommentText in onBindViewHolder它只能爲最後的評論(我知道這是因爲我通過循環所有的評論,並在同一holder.postCommentTextholder.postCommentName坐在這裏一一爲我onBindViewHolder文本..

@Override 
public void onBindViewHolder(FeedViewHolder holder, int position) 
{ 
    FeedParser.DataClass feedModel = feedList.get(position); 
    holder.tvFeedUsername.setText(feedModel.getName()); 
    holder.tvFeedCaption.setText(feedModel.getPost_status()); 

    if (getItemViewType(position) == TEXT_WITH_COMMENTS) 
    { 
     for (int commentNum = 0; commentNum < feedModel.getComments().size(); commentNum++) 
     { 
      holder.postCommentName.setText(feedModel.getComments().get(commentNum).getUser_name()); 
      holder.postCommentText.setText(feedModel.getComments().get(commentNum).getComment()); 
     } 
    } 
} 

我不知道即使這是正確的方法或不......但這是一些人在這裏寫了一個答案...像This one。我搜索了很多SO帖子,但沒有得到我想要的。有沒有其他方法可以使評論的XML佈局充氣並在RecyclerView的項目上多次添加它?

謝謝任何​​幫助,將不勝感激!

回答

0

我相信你需要創建在主回收視圖嵌套回收視圖,您需要將數據傳遞到嵌套適配器作爲。所以基本上

1)主要回收站視圖包含標題的評論,圖片,RecyclerView徵求意見(按照Instagram的)

2)在第二reyclerview把你的上述評論的佈局和創建數據一個單獨的適配器評論

那它..向我要任何進一步的幫助

+1

child Recycl在Main RecyclerView項目中查看erView?你是否認爲它對我來說是正確的,因爲我只需要顯示最後3條評論,而沒有別的!難道你不這樣更好地膨脹一個XML?我真的不知道instagram或其他人是如何做到這一點的。 –

+0

如果您必須循環瀏覽一些項目(在您的案例評論中),您必須創建一個列表或reyclerView(推薦)來顯示它們。那麼我有Android的發展經驗,我可以告訴你,這是去做的方式。別擔心,你會好起來的 –

+0

好吧,我會盡力...並儘快回覆你。 –

相關問題