如何销毁RecyclerView中的所有Web视图?

时间:2020-02-20 07:16:38

标签: audio webview android-webview android-recyclerview

我有5个音频,我在适配器中传递,在onbind中,我将该URL加载到webview中,当我按“后退”按钮时,音频没有停止,我尝试使用webview,但是它在最后一个webview项目位置上起作用。 >

我有 5个嵌入声音云的URL 列表,我将其传递给 adapter

meditationFavSCAdapter = new MeditationFavSCAdapter(FavMeditationActivity.this, favMeditationList, this);
recyclerMeditationfav.setAdapter(meditationFavSCAdapter);

if(favMeditationList.size() > 0) {
  for(int i=0;i<favMeditationAPI.getData().size();i++) {
        favMeditationList.add(favMeditationAPI.getData().get(i));
  }
}
meditationFavSCAdapter.notifyDataSetChanged();

在recyclerview项目中,我声明了webview(xml项目)。然后在Onbind方法中,将该URL加载到Webview中。

videoWebView.getSettings().setJavaScriptEnabled(true);
videoWebView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
videoWebView.getSettings().setLoadWithOverviewMode(true);
videoWebView.getSettings().setUseWideViewPort(false);
videoWebView.getSettings().setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
//Load Url
videoWebView.loadData(favMeditationList.get(position).getEmbedCode(), "text/html" , "utf-8" );

用于销毁Webview

public void destroyWebView() {
        if(videoWebView != null) {
            videoWebView.loadUrl("");
            videoWebView.loadData("", "text/html" , "utf-8" );
            videoWebView.clearHistory();

            // NOTE: clears RAM cache, if you pass true, it will also clear the disk cache.
            // Probably not a great idea to pass true if you have other WebViews still alive.
            videoWebView.clearCache(true);

            // Loading a blank page is optional, but will ensure that the WebView isn't doing anything when you destroy it.
            videoWebView.loadUrl("about:blank");

            videoWebView.onPause();
            videoWebView.removeAllViews();

            videoWebView.destroy();

            // Null out the reference so that you don't end up re-using it.
            videoWebView = null;
        }
    }

我在用户按下“后退”按钮时调用此功能

@Override
    public void onBackPressed() {
        meditationFavSCAdapter.destroyWebView();
        super.onBackPressed();
    }

但是此代码破坏了回收站中的最后一个Webview。 因此,当我播放第一个Webview音频并按“后退”按钮时,该音频仍在后台播放,而当我播放最后一个音频(WebView)时,它会在按下时停止。 所以我需要销毁recyclerview中的所有webview。 但是我不知道如何...

0 个答案:

没有答案
相关问题