我正在一個社交網絡應用程序中,我需要顯示供稿給訂閱者..用於在列表中顯示供稿。我正在使用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_TYPE
到onCreateViewHolder
像
@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.postCommentText
和holder.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的項目上多次添加它?
謝謝任何幫助,將不勝感激!
child Recycl在Main RecyclerView項目中查看erView?你是否認爲它對我來說是正確的,因爲我只需要顯示最後3條評論,而沒有別的!難道你不這樣更好地膨脹一個XML?我真的不知道instagram或其他人是如何做到這一點的。 –
如果您必須循環瀏覽一些項目(在您的案例評論中),您必須創建一個列表或reyclerView(推薦)來顯示它們。那麼我有Android的發展經驗,我可以告訴你,這是去做的方式。別擔心,你會好起來的 –
好吧,我會盡力...並儘快回覆你。 –