Java 向RecyclerView'动态添加视图或膨胀布局;s项目
我正在开发一个社交网络应用程序,其中我需要向订户显示提要。。用于在列表中显示提要。我正在使用Java 向RecyclerView'动态添加视图或膨胀布局;s项目,java,android,android-recyclerview,Java,Android,Android Recyclerview,我正在开发一个社交网络应用程序,其中我需要向订户显示提要。。用于在列表中显示提要。我正在使用RecyclerView。到现在为止没问题。。。但当我不得不在提要上显示一些评论时,问题就来了。我正在尝试完成一个类似Instagram的布局,他们在主列表中显示一个图像和最多3-4条评论。我正在从服务器获取图像链接及其注释(JSON数组形式)。。有时我得到的注释数组大小为0,有时为1/2../10。我已经为包含2个TextView的评论版面创建了一个XML,并根据我的ViewType在FeedViewH
RecyclerView
。到现在为止没问题。。。但当我不得不在提要上显示一些评论时,问题就来了。我正在尝试完成一个类似Instagram的布局,他们在主列表中显示一个图像和最多3-4条评论。我正在从服务器获取图像链接及其注释(JSON数组形式)。。有时我得到的注释数组大小为0,有时为1/2../10。我已经为包含2个TextView
的评论版面创建了一个XML,并根据我的ViewType
在FeedViewHolder
中对其进行了扩展。。我的项目\u注释
看起来像
<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>
并将ITEM\u TYPE
传递给onCreateViewHolder
like
@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);
}
如果viewType
有注释,则在FeedViewHolder
中膨胀项目布局和注释布局
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;
}
}
我已经验证了这一点,但当我在onBindViewHolder
中为holder.postCommentName
和holder.postCommentText
设置文本时,它只为最后的注释设置文本(我知道这是因为我正在循环浏览所有注释,并在同一个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());
}
}
}
@覆盖
BindViewHolder上的公共无效(FeedViewHolder,内部位置)
{
FeedParser.DataClass feedModel=feedList.get(位置);
holder.tvFeedUsername.setText(feedModel.getName());
holder.tvFeedCaption.setText(feedModel.getPost_status());
if(getItemViewType(position)==带注释的文本)
{
对于(int commentNum=0;commentNum
我不知道这是否是正确的方法…但这就是一些人在这里写答案的方式…比如。我搜索了很多帖子,但没有得到我想要的。有没有其他方法可以扩大评论的XML布局,并将其多次添加到RecyclerView的项目中
谢谢,如果有任何帮助,我们将不胜感激!我相信您需要在主回收器视图中创建一个嵌套的回收器视图,并且您需要将数据作为注释的数据传递到嵌套适配器中。因此基本上
1.)主回收器视图包含标题、图像、可供评论的回收器视图(根据instagram)
2.)在第二个reyclerview中,放置上面的注释布局,并创建一个单独的适配器,将数据作为注释
就这样。。请向我寻求任何进一步的帮助主RecyclerView项中的child RecyclerView?你认为这对我来说是正确的吗?因为我只需要显示最后3条评论,其他什么都不需要!你不认为最好是膨胀xml吗?我真的不知道instagram或其他网站是如何做到这一点的。如果你必须循环浏览某些项目(在你的评论中),你必须创建一个列表或一个reyclerView(推荐)来显示它们。我有安卓系统开发的经验,我可以告诉你,这是解决问题的方法。别担心,你会没事的。。如果有用的话,一定要告诉我。祝你一切顺利!!1.
@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());
}
}
}