RecyclerView - 回调时视图不再可见 [英] RecyclerView - callback when view is no longer visible

查看:4603
本文介绍了RecyclerView - 回调时视图不再可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它的适配器和一个的LayoutManager RecyclerView。适配器有大约15个不同的ViewHolders。其中一个包含的WebView它加载外部内容(其中99%是YouTube上的视频外)。
问题是当适配器的任何视图失控的画面 - 视频的WebView继续播放和声音播放。不过,这对我来说可以接受的。

I have a RecyclerView with its Adapter and LayoutManager. Adapter has approximate 15 different ViewHolders. One of them contain WebView which loads external contents (99% of them are videos outside of YouTube). The problem comes when any View of Adapter gets out of the screen - video in WebView keeps playing and sound is playing. Still, that's acceptable for me.

当我移动到另一个活动的关键问题开始。视频的声音仍然是present。

The key problem starts when I move to another Activity. The sound of the video is still present.

有什么办法,当其任何子女的意见改变可见性状态(从意义显示屏上消失)?

Is there any way RecyclerView could notify me when any of its children views change visibility state (meaning disappears from display)?

推荐答案

要去对自己做出回应。最好的办法就是添加 RecyclerView.OnChildAttachStateChangeListener()我RecyclerView,然后处理与我的WebView活动时, onChildViewDetachedFromWindow(查看视图)被调用。

Gonna respond to myself. Best approach is add RecyclerView.OnChildAttachStateChangeListener() to my RecyclerView and then handle events with my WebView when onChildViewDetachedFromWindow(View view) is called.

例如:

mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            WebView webView = (WebView) view.findViewById(R.id.webview);
            if (webView != null) {
                webView.onResume();
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {
            WebView webView = (WebView) view.findViewById(R.id.webview);

            if (webView != null) {
                webView.onPause();

            }
        }
    });

这篇关于RecyclerView - 回调时视图不再可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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