1

When I am doing pagination with the nested scroll view it takes too much time, sometimes my app hang? Please tell me the right way to implement pagination with nested scrollview

Danish Farooq
  • 97
  • 1
  • 9

2 Answers2

3

Add this class in you package

    import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.View;

public class TouchDetectableScrollView extends NestedScrollView {

    OnMyScrollChangeListener myScrollChangeListener;

    public TouchDetectableScrollView(Context context) {
        super(context);
    }

    public TouchDetectableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchDetectableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (myScrollChangeListener!=null)
        {
            if (t>oldt)
            {
                myScrollChangeListener.onScrollDown();
            }
            else if (t<oldt){
                myScrollChangeListener.onScrollUp();
            }
            View view = (View) getChildAt(getChildCount()-1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()));
            if (diff == 0 ) {
                myScrollChangeListener.onBottomReached();
            }
        }
    }

    public OnMyScrollChangeListener getMyScrollChangeListener() {
        return myScrollChangeListener;
    }

    public void setMyScrollChangeListener(OnMyScrollChangeListener myScrollChangeListener) {
        this.myScrollChangeListener = myScrollChangeListener;
    }

    public interface OnMyScrollChangeListener
    {
            public void onScrollUp();
            public void onScrollDown();
            public void onBottomReached();
    }
}

Now use TouchDetectableScrollView instead NestedScrollView in your xml and java code. and set Listener like this:

TouchDetectableScrollView nestedScrollView=findViewById(R.id.nestedScrollView);
        nestedScrollView.setMyScrollChangeListener(new TouchDetectableScrollView.OnMyScrollChangeListener() {
            @Override
            public void onScrollUp() {

            }

            @Override
            public void onScrollDown() {

            }

            @Override
            public void onBottomReached() {
                // api call for pagination
            }
        });

do your pagination task in onBottomReached method

Suraj Vaishnav
  • 5,731
  • 3
  • 29
  • 40
1

1. Set nested scrolling enabled false of recycler view.

recyclerView.setNestedScrollingEnabled(false);

2. Add scroll listner to nested scrollview.

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (isFeedsFetchInProgress)
                    return;
                if(!mOnLastPage) {
                    int visibleItemCount = linearLayoutManager.getChildCount();
                    int totalItemCount = linearLayoutManager.getItemCount();
                    int pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();
                    if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                        //End of list
                        mPageToRequest++;
                        loader = LOADER.BOTTOM;
                        hitApiRequest(ApiConstants.REQUEST_TYPE.GET_REFERRAL_LIST);
                    }
                }
//                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                swipeRefreshLayout.setEnabled(scrollY <= 0);

            }
        });
Alok Singh
  • 516
  • 3
  • 13