Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView adapter taking wrong values during scrolling

I have read several questions on stackoverflow addressing this problem but I could not solve my problem. The recyclerview shows webviews. My issue is that the adapter, initially, load the right values (below image):

right behavior

but when I scroll down, and scroll up to first position again, wrong values are displayed in webview (below image):

right behavior

I think this problem may originate from previously started loads into the webview, but I don't have any idea about handling this behavior. Here is related parts of my code:

/* in my notation, pg is equivalent to page.*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ebook_list, container, false);
mEbookRecyclerView = (RecyclerView) view.findViewById(R.id.ebook_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
mEbookRecyclerView.setLayoutManager(mLayoutManager);
updateUI();
return view;
}

@Override
public void onResume() {
super.onResume();
updateUI();
}


private void updateUI() {
if (mAdapter == null) {
    EbookLab ebookLab = EbookLab.get(getActivity());
    List<String> pgs = ebookLab.getPgs();
    mAdapter = new EbookAdapter(pgs);
    mEbookRecyclerView.setAdapter(mAdapter);
}
}

   /* ******codes for preparation of menus which I have ignored *******/

private class EbookHolder extends RecyclerView.ViewHolder {
private String mPg;

public EbookHolder(LayoutInflater inflater, ViewGroup container) {
    super(inflater.inflate(R.layout.list_item_ebook, container, false));
    mListWebView = (WebView) itemView.findViewById(R.id.list_web_View);
    mListWebView.getSettings().setLoadWithOverviewMode(true);
}

public void bindEbook(String pg) {
    mPg = pg;
    mListWebView.loadDataWithBaseURL("file:///android_asset/", "Stack Over Flow " + String.valueOf(glbPos), "text/html", "UTF-8", null);

}
}

private class EbookAdapter extends RecyclerView.Adapter<EbookHolder> {
private List<String> mPgs;
public EbookAdapter(List<String> pgs) {
    mPgs = pgs;
}
@Override
public EbookHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    return new EbookHolder(layoutInflater, parent);
}


@Override
public void onBindViewHolder(EbookHolder holder, int position) {
    String pg = mPgs.get(position);
    glbPos = position;//A global variable for position
    holder.bindEbook(pg);
}
@Override
public int getItemCount() {
    return mPgs.size();
}
public void setPgs(List<String> pgs) {
    mPgs.clear();
    mPgs.addAll(pgs);
    notifyDataSetChanged();
}
}

Any helps is appreciated.

Update:In my code , I defined mListWebView as a global variable. But it should be defined inside private class EbookHolder extends RecyclerView.ViewHolder. Then I removed bindEbook(String pg) method and directly updated mListWevView inside public void onBindViewHolder(EbookHolder holder, int position):

holder.mListWebView.loadDataWithBaseURL("file:///android_asset/", "Stack Over Flow " + String.valueOf(glbPos), "text/html", "UTF-8", null);

My Question >> Does calling bindEbook(String pg) takes longer time compared to scrolling recyclerview or it is related to Android Architecture??

like image 421
Mr. Nobody Avatar asked Jan 31 '26 11:01

Mr. Nobody


1 Answers

Please try few things:

1) put log after String pg = mPgs.get(position); and see, which pg value is after that line;

In bindEbook():
2) put log to see which pg value is incoming;
3) put mListWebView.clearHistory() or mListWebView.loadUrl("about:blank") before mListWebView.loadDataWithBaseURL() just to see is your webview really loading new page

Also - try to replace webview with simple textView - you will see, is problem in webview or in adapter.

like image 53
Goltsev Eugene Avatar answered Feb 03 '26 04:02

Goltsev Eugene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!