• RecyclerView解析--onViewDetachedFromWindow()/onViewAttachedToWindow()


    先看这段源码介绍:

    /**
             * Called when a view created by this adapter has been detached from its window.
             *
             * <p>Becoming detached from the window is not necessarily a permanent condition;
             * the consumer of an Adapter's views may choose to cache views offscreen while they
             * are not visible, attaching an detaching them as appropriate.</p>
             *
             * @param holder Holder of the view being detached
             */
            public void onViewDetachedFromWindow(VH holder) {
            }

    Called when a view created by this adapter has been detached from its window.
    当适配器创建的view(即列表项view)被窗口分离(即滑动离开了当前窗口界面)就会被调用)

    这个方法就是用来当你的列表项滑出可见窗口之外的时候,需要重写此方法进行相应的一些操作。

    -----------------------------------------------------------------------------------------------------------------

    这个方法具体什么时候用呢?

    比如:

    我有一个列表,列表的每一个列表项里面都要播放一个短视频,这时候,当我滑动一个列表项直至它消失在可视界面时,便会调用onViewDetachedFromWindow()方法,重要的一点,视频控件也会执行它自己的onViewDetachedFromWindow()方法,那么此时我再滑动回来,让该列表项出现在当前界面,会发现视频那一部分就是黑屏或者白屏了。

    注意,出现这个Bug的条件是,该列表项滑动出可视界面,但是滑动距离不长,因为长的话,你再滑回来就会复用View执行onBindViewHolder()方法。

    解决方法就是在RecyclerView中重写onViewDetachedFromWindow()方法,对视频进行一个相应的操作(初始化等等)。

    -----------------------------------------------------------------------------------------------------------------

     对应方法:onViewAttachedToWindow() 

     当列表项出现到可视界面的时候调用

    /**
             * Called when a view created by this adapter has been attached to a window.
             *
             * <p>This can be used as a reasonable signal that the view is about to be seen
             * by the user. If the adapter previously freed any resources in
             * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow}
             * those resources should be restored here.</p>
             *
             * @param holder Holder of the view being attached
             */
            public void onViewAttachedToWindow(VH holder) {
            }
  • 相关阅读:
    取字符串前缀
    分解质因数
    git 常用命令
    微信 iphone端 audio 播放问题
    git入门:创建合并分支 解决冲突 分支管理策略
    git入门:远程仓库 github篇
    git入门:撤销修改 删除文件
    git入门: 工作区暂存区 以及 管理修改
    函数柯里化实现
    转载:深度工作:充分使用每一份脑力
  • 原文地址:https://www.cnblogs.com/xqxacm/p/5213096.html
Copyright © 2020-2023  润新知