如何让 RecyclerView 停止回收定义的位置? [英] How to make RecyclerView stops recycling defined positions?

查看:27
本文介绍了如何让 RecyclerView 停止回收定义的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我在 RecyclerView 内的一个视图上发生了视频流.

My problem is: I have a video streaming happening on one of the views inside the RecyclerView.

当用户滚动时,视图会被回收,而其他相机会在该回收的 Viewholder 上开始自己的流式传输.这对用户界面不利,因为流式传输过程需要几秒钟才能启动.

When the user scrolls, the view gets recycled and other cameras starts their own streaming on that recycled viewholder. This is bad for user interface since the streaming process takes some seconds to start.

我如何对 RecyclerView 说:嘿,Recycler,请不要回收那个确切的位置 x 并给该位置 始终 与您给它相同的视图第一次,而不是随机一次"?

How can I say to the RecyclerView: "Hey Recycler, please, do not recycle that exact position x and give that position ALWAYS the same viewholder you gave it the first time, instead of random one"?

请有人帮我=(

推荐答案

在你的 getItemViewType(int position) 适配器方法中,为每个视频分配唯一的值,所以它总是返回相同的 ViewHolder与您希望的相同视频.

In your getItemViewType(int position) method of adapter, assign unique values for each video, so it will always return same ViewHolder for same video as you wish.

  • 为每种视频类型返回唯一的正数作为类型(这里我使用适配器位置作为唯一键)
  • 为任何非视频项目返回负数.(这里没什么特别的,只是为了避免与视频项目发生冲突,我们对非视频项目使用负数)

我希望你能明白.干杯:)

I hope you get the idea. cheers :)

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be   contiguous
        if(dataList.get(position).isVideo()){
            return position;

        }else{
            return -1;//indicates general type, if you have more types other than video, you can use -1,-2,-3 and so on.
        }
    }

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case -1:  View view1 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.general_item, parent, false);
                     return new GeneralViewHolder(view1);
             default:View view2 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.video_item, parent, false);
                     return new VideoViewHolder(view2);

         }
    }

这篇关于如何让 RecyclerView 停止回收定义的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆