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
Asked
Active
Viewed 6,408 times
1

Danish Farooq
- 135
- 1
- 10
-
you mean pagination in `RecyclerView`? – pskink Jan 18 '18 at 11:49
-
My Recyclerview is inside in NestedScrollView So I get action response from Nestedscrollview – Danish Farooq Jan 18 '18 at 11:52
-
sorry, i have no idea what you are trying to achieve – pskink Jan 18 '18 at 11:53
-
I handled all pagination with nestedscrollview action.. because nestedscrollview is my parent view . – Danish Farooq Jan 18 '18 at 11:55
-
Check this [answer](https://stackoverflow.com/a/46638845/5308778), I think this is what you are trying to achieve. – yashkal Jan 18 '18 at 12:02
-
1Your Recyclerview taking so much time because it is inside NestedScrollView and it is getting the full length. So it is keep calling Next Page automatically till the end (You can check logs). – Satwinder Singh Sep 21 '18 at 11:16
2 Answers
5
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
- 7,777
- 4
- 43
- 46
-
-
Might be internet issue, which library you are using for API call? – Suraj Vaishnav Jan 18 '18 at 12:17
-
No this is not internet issue . I am using Retrofil library for making connection – Danish Farooq Jan 18 '18 at 12:24
-
your Custom nestedScrollview works perfectly fine but not working in ViewPager tab ? – Ahsan Syed Mar 08 '21 at 11:16
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
- 640
- 4
- 14