8

In my Nougat device, webview inside RecyclerView is blank sometimes. When I scroll slowly and then go back to webview item content disappear. There is no issue on devices below Android N. Android N uses Chrome as the default browser for apps. So I thought there might be a bug in Chrome so I raise a bug in chrome portal as well. There are a couple of related question in SO but that didn't solve my problem. So is there a way in Android webview setting which can solve this problem? I have written detail description in the bug link.

Bug link: click here

My onBindViewHolder method code for WebView is

final VHItem vhItem = (VHItem) holder;

vhItem.webViewChild.getSettings().setUseWideViewPort(false);
vhItem.webViewChild.getSettings().setJavaScriptEnabled(true);

vhItem.webViewChild.loadData("<body>" + html + "</body>", "text/html;charset=utf-8", "utf-8");

where

html is the html string

Update

They have fixed the issue. If you are still having the same problem try updating your Android chrome version to 61 or above.

Kunu
  • 5,078
  • 6
  • 33
  • 61

4 Answers4

3

For a start webview consumes memory because it load has to load and render html data. Rather than using a webview in a recycler view, I think it would be better if you implemented it either of these two ways:

  1. You handle the list of data in html and send it into the webview and remove the recycler view completely
  2. You draw the layout of the expected contents in xml and inflate it directly into the recyclerview and remove webview completed. (Note: you can inflate different views into a recycler depending on the adapter position and data at that position).

Using a webview might seem the easy way to implement whatever you are trying but trust me, the drawbacks outweight the benefit. So its just best to avoid it.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Odufuwa Segun
  • 274
  • 1
  • 9
  • 2
    WebView in RecyclerView is manageable if you implement an application scoped cache and create the WebView with application context. That way you don't have to reinitialize it on each scroll into viewport or each screen orientation change. – Eugen Pechanec Aug 02 '17 at 08:14
  • 1
    @EugenPechanec Can you elaborate a little? – Kunu Aug 03 '17 at 11:36
  • 1
    Yes please can u tell more on how to manage webviews with application scope. – Harsh Aug 15 '17 at 08:38
1

This problem can arrive for many possible reasons

  • When you scroll very fast Recyclerview is purely based on Inflating the view minimal times and reusing the existing views. This means that while you are scrolling when a view(An item) exits your screen the same view is bought below just by changing its contents.When you load from internet it's always better to first download all the data and then display it. Webview's consume a lot of data and its totally Against the design principle to have them in a Recyclerview.

To repair this you could possibly add some button to reload data or refresh each time you display the view.

  • Nougat removed some functions from http urlconnection class I am not sure about this one. But in one of google developer video ,T had seen something about depreciation of some functions and methods

hope You find this Helpful.

jeevan s
  • 631
  • 9
  • 17
0

I solved this problem by floating a WebView in a up layer of RecyclerView, meanwhile placing a HOLDER view in RecyclerView. And then I register an listener to the scroll event of RecyclerView. I Control the position and visibility of the floating WebView

0

You can try this code in onCreateViewHolder

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        WebView web = new WebView(parent.getContext());
                        web.setLayoutParams(lp);
                        String url="...";
                        WebSettings settings = web.getSettings();
                        settings.setJavaScriptEnabled(true);
                        settings.setDomStorageEnabled(true);

                        web.loadUrl(url);
                        holder = new ViewHolder(web);
ARR.s
  • 769
  • 4
  • 20
  • 39