0

I used WebView to load data html for each item of RecyclerView. I have problems when did it:

  1. Some time WebView display fail data. When I scrolling RecyclerView some item will show data of another item.[Solved]

  2. Height of webview fail. Some item will hold height of another item in RecyclerVew.

How I can fix them? Here is my adapter:

class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.MyViewHolder> {
    private String[] urlList;
    public ParentAdapter(String[] urlList) {
        this.urlList = urlList;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_my_web, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final @NonNull MyViewHolder holder, int position) {
        holder.myWebView.loadData(content + styleWebView, "text/html", "UTF-8");
    }

    @Override
    public int getItemCount() {
        return urlList.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        WebView myWebView;
        TextView tv;

        MyViewHolder(View itemView) {
            super(itemView);
            myWebView = itemView.findViewById(R.id.myWebView);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            webSettings.setAppCacheEnabled(false);
            myWebView.setWebChromeClient(new WebChromeClient());
            myWebView.addJavascriptInterface(new WebAppInterface(myWebView.getContext()), "Android");
            myWebView.setWebViewClient(new WebViewClient());
        }
    }

    class WebAppInterface {
        Context mContext;

        WebAppInterface1(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void clickMention(String idMention) {
            Toast.makeText(mContext, idMention, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void clickPhone(String phone) {
            Toast.makeText(mContext, phone, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void clickEmail(String phone) {
            Toast.makeText(mContext, phone, Toast.LENGTH_SHORT).show();
        }
    }
My row_my_web.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#888585"
    android:orientation="vertical"
    android:paddingBottom="10dp">
    <WebView
        android:id="@+id/myWebView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff" />
</LinearLayout>
Hoa.Tran
  • 935
  • 10
  • 26
  • What did you do to make problem #1 work? I am seeing a case where a re-used ViewHolder refuses to update its WebView's content, even though it updates 3 TextViews. – chrisbtoo Aug 23 '19 at 02:49

1 Answers1

0

Answer to your first question :

Try this solution :

RecyclerView items duplicate and constantly changing

Maybe this solution will fix your second problem as well. Do let me know if not solved !

Vikas
  • 136
  • 6
  • thank you so much. I found my bug when call loadmore item so I fixed first question. Now I find solution for two question. Thank you! – Hoa.Tran May 27 '19 at 08:40