在垂直Recyclerview内部的水平RecyclerView无效,onCreateViewHolder从未调用过

时间:2017-03-31 14:46:17

标签: android android-recyclerview recycler-adapter

我正在显示像facebook一样的帖子,在每个帖子中都有一个带有多个图像的图像滑块,在我的列表中第一个项目与其他项目不同,所以我使用异构的RecyclerView,这里我做了什么请看我的代码,

    if (commentList.get(position).getAttachment() != null && !commentList.get(position).getAttachment().equals(""))
    {
        List<String> imageUrl = new ArrayList<>();
        imageUrl.add(commentList.get(position).getAttachment());
        holder.getImageSliderCommentRecycler().setAdapter(new ImageSliderAdapter(context, imageUrl, new ImageSlideItemListener()
        {
            @Override
            public void imageClicked(int position)
            {
                List<String> imageUrl = new ArrayList<>();
                imageUrl.add(commentList.get(holder.getAdapterPosition()-1).getAttachment());
                listener.showImage(imageUrl, position);
            }
        }));

        holder.getImageSliderCommentRecycler().setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    }
    else holder.getImageSliderCommentRecycler().setVisibility(View.GONE);

如果条件执行但imageSliderAdapter从不调用oncreateViewHolder或其他任何东西......这里是xml代码请看,

      <RelativeLayout
            android:id="@+id/post_photo_relative_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/comment_textview"
            android:layout_marginEnd="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="35dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/about_doctor_fragment_doctor_photos_fragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

        </RelativeLayout>

这是我的adapterViewHolder ...

public class CommentViewHolder extends RecyclerView.ViewHolder
{
private ImageView commentProfileImage;
private TextView commentName;
private TextView commentSpeciality;
private ImageView commentDeleteIcon;
private Button bookAppointmentButton;
private TextView commentDescription;
private TextView commentDateTime;
private ImageView commentLikeIcon;
private TextView commentLikeCounter;
private Button replyButton;
private RecyclerView replyRecyclerView;
private RecyclerView imageSliderCommentRecycler;

public CommentViewHolder(View itemView)
{
    super(itemView);

    commentProfileImage = (ImageView) itemView.findViewById(R.id.photo_imageview);
    commentName = (TextView) itemView.findViewById(R.id.user_name_textview);
    commentSpeciality = (TextView) itemView.findViewById(R.id.user_speciality_textview);
    commentDeleteIcon = (ImageView) itemView.findViewById(R.id.delete_comment_imageview);
    bookAppointmentButton = (Button) itemView.findViewById(R.id.book_appointment_button);
    commentDescription = (TextView) itemView.findViewById(R.id.comment_textview);
    commentDateTime = (TextView) itemView.findViewById(R.id.comment_time_textview);
    commentLikeIcon = (ImageView) itemView.findViewById(R.id.like_imageview);
    commentLikeCounter = (TextView) itemView.findViewById(R.id.likes_count_textview);
    replyButton = (Button) itemView.findViewById(R.id.reply_button);
    replyRecyclerView = (RecyclerView) itemView.findViewById(R.id.replies_recyclerview);

    imageSliderCommentRecycler = (RecyclerView) itemView.findViewById(R.id.about_doctor_fragment_doctor_photos_fragment);
}

public ImageView getCommentProfileImage()
{
    return commentProfileImage;
}

public TextView getCommentName()
{
    return commentName;
}

public TextView getCommentSpeciality()
{
    return commentSpeciality;
}

public ImageView getCommentDeleteIcon()
{
    return commentDeleteIcon;
}

public Button getBookAppointmentButton()
{
    return bookAppointmentButton;
}

public TextView getCommentDescription()
{
    return commentDescription;
}

public TextView getCommentDateTime()
{
    return commentDateTime;
}

public ImageView getCommentLikeIcon()
{
    return commentLikeIcon;
}

public TextView getCommentLikeCounter()
{
    return commentLikeCounter;
}

public Button getReplyButton()
{
    return replyButton;
}

public RecyclerView getReplyRecyclerView()
{
    return replyRecyclerView;
}

public RecyclerView getImageSliderCommentRecycler()
{
    return imageSliderCommentRecycler;
}

}

代码非常复杂,因此我只发布块,因为其他一切正常....这个图像滑块我刚刚在前一天添加,当时它工作正常,但现在它不是......

任何帮助都将不胜感激......提前致谢

1 个答案:

答案 0 :(得分:1)

经过2天的挣扎后,我终于弄明白出了什么问题....问题是XML,我使用相同的Id用于两个不同的RecyclerViews,所以它总是找到第一个recyclerview并设置其适配器并忽略第二个....我之所以没有意识到编译器的真正原因是因为这两个recyclerviews都在父相对布局容器中,这些不是兄弟姐妹。我还使用&#39; include&#39;添加了第一个recyclerview。标签

这就是我的XML完全看起来像什么

setMaximumSize

以下是代码的其余部分......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_post_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="controllers.marham.marhammed.activities.DetailPostActivity">

<RelativeLayout
    android:id="@+id/topcolor_red"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:background="@color/light_blue">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/forum"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />

    <ImageButton
        android:id="@+id/backBtn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:background="@color/light_blue"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/backicon"/>


</RelativeLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/post_detail_activity_recyclerview"
    android:layout_below="@+id/topcolor_red"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ScrollView
    android:id="@+id/post_detail_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <include
        layout="@layout/layout_post_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</ScrollView>

</RelativeLayout>

我发布这个,以便其他人可以节省一些时间。感谢您阅读... :)