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

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

问题描述

我的问题是:RecyclerView内部的一个视图上正在发生视频流传输.

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

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

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天全站免登陆