0

I have a fragment with parent RecyclerView and his child (the child is inner recyclerview). When I scroll to bottom (last data), and scroll it again to top, I found the problem. One of data is missing. When I try to scroll to bottom again and scroll to top, the data is random. Anyone can help me?

HomeFragment

    GridLayoutManager gl = new GridLayoutManager(getContext(), 1, 
    GridLayoutManager.VERTICAL, false);
    ParentAdapter adapter = new ParentAdapter(getContext(), parentList);
    rcvParent.setAdapter(adapter);

OnBindViewHolder

    holder.title.setText(parentList.get(position).getTitle());
    if (parentList.get(position).getProducts() != null) {
        for (ProductModel pm : parentList.get(position).getProducts()) {
            productList.add(pm);
        }
        GridLayoutManager childLayout = new GridLayoutManager(mContext, 1, GridLayoutManager.HORIZONTAL, false);
        holder.childRecyclerView.setLayoutManager(childLayout);
        ChildAdapter ca = new ChildAdapter(mContext, productList);
        holder.childRecyclerView.setAdapter(ca);
    } else {
        holder.childRecyclerView.setVisibility(View.GONE);
    }

ScreenShoot

enter image description here

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Husni Kamal
  • 75
  • 1
  • 8

1 Answers1

2

When you scroll to bottom and again to top. the OnBindViewHolder method recall, so the inner Recyclerview will set adapter another time.

It's better to use NestedScrollView as a parent of fragment instead of RecyclerView

you may face a problem that the recyclerview slow when scroll. use this code to solve it and enhance the performance.

 recycleview.setNestedScrollingEnabled(false);
 ViewCompat.setNestedScrollingEnabled(recycleview, false);
 recycleview.setHasFixedSize(true);
 recycleview.setItemViewCacheSize(20);
 recycleview.setDrawingCacheEnabled(true);
 recycleview.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Now, you should use the code in the method OnBindViewHolder inside the onCreateView in your fragment to set the adapter for the inner recyclerView

tell me if you have any problem

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
  • I use recyclerview within recylerview cause the response is array within array sir, and it is dynamic, so I think should use inner recyclerview. any idea sir? – Husni Kamal Aug 24 '18 at 02:11
  • it works sir. I updated my inner recyclerview and set fragment layout to `NestedScrollView`, but the scroll is not smooth :( – Husni Kamal Aug 24 '18 at 02:18
  • yes, you should use these code `recycleview.setNestedScrollingEnabled(false); ViewCompat.setNestedScrollingEnabled(recycleview, false); recycleview.setHasFixedSize(true); recycleview.setItemViewCacheSize(20); recycleview.setDrawingCacheEnabled(true); recycleview.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);` – Momen Zaqout Aug 24 '18 at 02:30
  • I've write that code sir, but recyclerview scroll little by little :( I can't scroll to end with one scroll :( – Husni Kamal Aug 24 '18 at 02:49
  • I put the code in `OnBindViewHolder` sir. Is that right? – Husni Kamal Aug 24 '18 at 02:51
  • I'm sorry :( I made a mistake sir. Thanks for your help! :) – Husni Kamal Aug 24 '18 at 06:29
  • How can we set the adapter for inner recyclerview from onCreateView of fragment? Can you provide any code or link for more reference @MomenZaqout – Parsania Hardik Aug 02 '23 at 05:51