1

I have a Recyclerview, inside NestedScrollview. Recyclerview is having different item types that need to load dynamically,But all items in recyclerview are binding together when it is inside nested scroll. How can I avoid this? I don't need any pagination. I just want to minimize the view rendering.

Anu
  • 1,195
  • 2
  • 19
  • 35
  • Possible duplicate of [Pagination not work for the RecyclerView within NestedScrollView](https://stackoverflow.com/questions/46638779/pagination-not-work-for-the-recyclerview-within-nestedscrollview) – Vishva Dave Feb 22 '18 at 12:16
  • possible duplicate https://stackoverflow.com/questions/31000081/how-to-use-recyclerview-inside-nestedscrollview/33923207 – duggu Feb 22 '18 at 12:17
  • do you resolve your problem ? can you share your solution :) – Adnan Abdollah Zaki Sep 24 '18 at 10:28

1 Answers1

0

Try this code:

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;

import android.view.View;


public abstract class RecyclerViewByNestedScrollListener implements NestedScrollView.OnScrollChangeListener {
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;
    // The minimum amount of pixels to have below your current scroll position
    // before loading more.
    private int visibleThresholdDistance = 500;

    RecyclerView.LayoutManager mLayoutManager;

    public RecyclerViewByNestedScrollListener(RecyclerView.LayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
    }

    @Override
    public void onScrollChange(NestedScrollView scrollView, int x, int y, int oldx, int oldy) {
        // We take the last son in the scrollview
        View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
        int distanceToEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

        int totalItemCount = mLayoutManager.getItemCount();
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }

        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        // threshold should reflect how many total columns there are too
        if (!loading && distanceToEnd <= visibleThresholdDistance) {
            currentPage++;
            onLoadMore(currentPage, totalItemCount);
            loading = true;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount);

}

and then use NestedScrollView as parent of your recyclerView. Then in code do like this:

LinearLayoutManager layoutManager=new LinearLayoutManager(this);

NestedScrollView mScrNested=(NestedScrollView)findViewById(R.id.nestedScrollView);

mScrNested.setOnScrollChangeListener(new RecyclerViewByNestedScrollListener(layoutManager) {
                @Override
                public void onLoadMore(int page, int totalItemsCount) {
                    //load next page of your recyclerView
                }
            });

As you see at above, you must set load more of your recyclerview with nestedScorllView. Otherwise your data load at once and your view has lagged. All of this problem is because recyclerView not work properly inside ScrollView.

** Updated answer **

Try using Handler like below for delaying add each item to your recyclerview:

handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    yourList.add(newItem);
                    adapter.notifyDataSetChanged();

                }
            },random);//set your delay time here like 2000 for two seconds delay
Masoud Mokhtari
  • 2,037
  • 1
  • 12
  • 38