Android 如何不只是回收我的recyclerview或任何项目的前2个视图?

Android 如何不只是回收我的recyclerview或任何项目的前2个视图?,android,android-layout,android-fragments,android-recyclerview,android-videoview,Android,Android Layout,Android Fragments,Android Recyclerview,Android Videoview,我正在使用recyclerview来显示和播放用户的视频。然而,当我在recycler视图中滚动时,我看到了我的第一个视图,其中我的视频被回收,因此其他用户看不到它。有没有办法确保第一个视图不被回收,这样我就不用担心每次滚动列表时我的视频视图都会被回收 这是我的密码: 在我的片段中。。。 项目组人员: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmln

我正在使用recyclerview来显示和播放用户的视频。然而,当我在recycler视图中滚动时,我看到了我的第一个视图,其中我的视频被回收,因此其他用户看不到它。有没有办法确保第一个视图不被回收,这样我就不用担心每次滚动列表时我的视频视图都会被回收

这是我的密码: 在我的片段中。。。

项目组人员:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp"
    android:background="@drawable/person_border"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:id="@+id/personContainer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">

        <FrameLayout
            android:id="@+id/personVideo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black" />

        <ImageView
            android:id="@+id/personImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:src="@drawable/default_profile" />

        <TextView
            android:id="@+id/personName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#AA555555"
            android:gravity="center_horizontal|bottom"
            android:textColor="@color/green"
            android:textSize="12sp"
            android:lines="1"
            android:ellipsize="end"
            tools:text="androiduser@gmail.com"
            android:layout_alignParentBottom="true" />

    </RelativeLayout>




</android.support.constraint.ConstraintLayout>

具有循环视图的片段:xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout>
....
</RelativeLayout>
<include containers>...</include>
...
 <android.support.v7.widget.RecyclerView
        android:id="@+id/personList"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="invisible"
       >

    </android.support.v7.widget.RecyclerView>
 </android.support.constraint.ConstraintLayout>

....
...
...
我认为“不回收”视频视图实际上不会阻止它在滚动时被破坏。问题不在于回收利用,而在于解除束缚的观点

我认为像
VideoView
这样复杂的组件根本不应该出现在
RecyclerView
中。。您可以尝试将其作为静态内容添加到顶部,这很可能会解决问题。您可以使用
NestedScrollView
进行此操作。请看这里:

如果您仍然认为要将其保留在
回收视图中并禁用回收,请执行以下操作

为视频视图项目创建单独的视图类型。以下是一个例子: . 将视频视图项视为示例中的标题视图

执行此操作后,将有一个
RecycledViewPool
类,用于管理
RecyclerView
中项目的回收。您可以告诉它哪些视图需要回收,哪些不需要回收。默认情况下,它会回收所有视图。要禁用视频视图项目的回收,请使用新的视图类型,如下所示:

recyclerView.getRecycledViewPool().setMaxRecycledViews(TYPE_VIDEO_VIEW, 0);
其中
TYPE\u VIDEO\u VIEW
是您使用上一示例创建的新类型。数字0告诉
RecyclerView
要回收的项目数量-在本例中为0,表示“不回收”。有关此的详细信息,请单击此处:


我希望这有帮助。。祝你好运

答案是肯定的,你可以做到。 因为你说的是“第一项”,所以你只需加一张支票

if(position == 0)
{
holder.setIsRecyclable(false); //This line prevents the row from recycling
}

是否只有您的第一个项目包含VideoView?其他项目也包含VideoView,但只有第一个项目会播放自己的视频谢谢您的回复@Gennadii然而,我的代码并不希望以任何一种方式进行更改。我现在已经发布了全部代码片段。实际上,我正在从createviewholder中的适配器中扩展布局“item_person”,而我的recyclerview是xml片段中视图的一部分,当人们加入房间时,它分别填充每个“item_person”。我尝试了嵌套的滚动视图,但在我的情况下很难让它工作,对于另一个有点复杂的部分也是如此。你能解释一下我的代码吗?这将非常有用。另外,第二种方法中我面临的唯一问题是在oncreateviewholder中设置viewtype。不确定是否需要为两种不同的布局充气?或者使用同一个。我有点好奇,第一种方法出了什么问题?在第二种方法中,如果您愿意,您可以膨胀2种不同的布局,但这不是义务。重要的部分是确保视频项与类型1匹配,其他项与类型2匹配。然后您可以禁用类型1的回收,这将仅对视频项目关闭。可能我做错了什么,但在NestedScroll视图中添加recyclerview会导致崩溃和问题。正如您从我的代码中所看到的,这并不简单,除非我弄错了。另外,仔细想想,是否可以不回收位置0处的第一个项目,因为它是唯一一个广播/流式传输视频的项目,其余项目只显示视频。当我尝试设置可回收(false)时对于bindviewholder中的位置0,它未按预期工作,并且在视图不可见时仍会循环使用。你能参考我的代码并提出一些可以轻松更改的建议来解决这个问题吗?我只是想澄清一下,我并不认为禁用回收会首先解决这个问题,这就是为什么我建议使用
NestedScrollView
。循环使用=滚动时重复使用视图。即使您禁用它,视图仍将在不可见时被销毁,因为这就是
RecyclerView
的工作方式。另一方面,
NestedScrollView
只是滚动它的内容,它不会破坏任何东西,这就是为什么它应该解决这个问题。因此,我认为您仍然应该使用
NestedScrollView
来调查崩溃的原因。我应该在哪里添加这个?在bindview支架上?或者其他地方?您可以将其添加到onBindViewHolder的开头。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout>
....
</RelativeLayout>
<include containers>...</include>
...
 <android.support.v7.widget.RecyclerView
        android:id="@+id/personList"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="invisible"
       >

    </android.support.v7.widget.RecyclerView>
 </android.support.constraint.ConstraintLayout>
recyclerView.getRecycledViewPool().setMaxRecycledViews(TYPE_VIDEO_VIEW, 0);
if(position == 0)
{
holder.setIsRecyclable(false); //This line prevents the row from recycling
}