Nested Recyclerview Issues

会有一股神秘感。 提交于 2021-01-29 05:28:59

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/51995625/nested-recyclerview-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!