3

I have 2 horizontal and 1 vertical (Grid layout) in a single Scrollview.

<ScrollView> <Horizontal RecyclerView/> <Horizontal RecyclerView/> <Vertical RecyclerView/> </ScrollView> 

Above is the schematic of my view.

I have to load data on once the vertical recyclerviews last item is visible, but i'm unable to get the scroll event for my vertical view.

Here is my scrollviewlistener.

 recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); totalItemCount = gridLayoutManager.getItemCount(); lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition(); if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { // End has been reached // Do something if(onLoadMoreListener!=null) { onLoadMoreListener.onLoadMore(lastVisibleItem); } loading = true; } } }); 

but this setonscrollListener is never fired. How do I get scroll event for above case.

Thanks in advance:)

2
  • I don't think is the best idea to have nested scrollable views (like a RecyclerView inside the ScrollView), though this is now supported since API 21. But anyway, take a look at this, it could help: stackoverflow.com/questions/6210895/… Commented Sep 21, 2015 at 14:21
  • it's a little hard to use two scrollable View inside each other, and it's become harder when you want them flexible. Commented Oct 21, 2015 at 7:07

3 Answers 3

6

I had the problem here is how to solve it:

  1. First create an Interface for scroll listener

    public interface EndlessScrollListener { void onScrollChanged(EndlessScrollView scrollView,int x, int y, int oldx, int oldy); } 
  2. Create a Custom ScrollView by extending the ScrollView

    public class EndlessScrollView extends ScrollView { private EndlessScrollListener endlessScrollListener = null; public EndlessScrollView(Context context) { super(context); } public EndlessScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public EndlessScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(EndlessScrollListener endlessScrollListener) { this.endlessScrollListener = endlessScrollListener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (endlessScrollListener != null) { endlessScrollListener.onScrollChanged(this, l, t, oldl, oldt); } } } 
  3. Then in your Fragment/Activity layout use the EndlessScrollView instead of Default one:

    <YOUR_PACKAGE_NAME.EndlessScrollView android:id="@+id/my_scroll_view"> <!-- your recycler views ... --> </YOUR_PACKAGE_NAME.EndlessScrollView> 
  4. Next your Activity/Fragment should implement the Interface your created in step 1.

  5. set your Activity/Fragment as Scroll Listener and then use like this:

    public class YourActivity extends Activity implements EndlessScrollListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EndlessScrollView myScrollView = (EndlessScrollView) findViewById(R.id.my_scroll_view); myScrollView.setScrollViewListener(this); } @Override public void onScrollChanged(EndlessScrollView 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())); // if diff is zero, then the bottom has been reached if (distanceToEnd == 0) { // do stuff your load more stuff } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, it did help me. I will surely recommend this. Thanks man
2

Use the gesture detector to get the scroll event in RecyclerView.

EDIT Replacing ScrollView with NestedScrollView solved my problem of recyclerview scrolling.

final NestedScrollView parentScrollView=(NestedScrollView)view.findViewById (R.id.parentScrollView); parentScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { // Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY); //Do something } } }); 

Comments

0

when you use tow scrollble element inside each other you are in hot water! you should calculate the Recycler item height and then find the whole recycler height. look at below link, I explain completely this problem.

Use RecyclerView indie ScrollView with flexible recycler item height

I hope it help you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.