Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview inside RecyclerView is showing blank screen sometimes on Nougat devices only

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.

like image 758
Kunu Avatar asked Jul 25 '17 10:07

Kunu


People also ask

Can we use RecyclerView inside RecyclerView in Android?

A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations.

Is RecyclerView deprecated?

It was removed in version 7.0 of the Android Gradle plugin.

What is RecyclerView setHasFixedSize?

setHasFixedSize(true) means the RecyclerView has children (items) that has fixed width and height.

What is RecyclerView ViewHolder in Android?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView.


1 Answers

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.

like image 132
Odufuwa Segun Avatar answered Oct 21 '22 18:10

Odufuwa Segun