scroll event for recyclerview inside scrollview android

故事扮演 提交于 2019-12-05 19:39:07

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
    
            }
        }
    }
    

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
                            }
                    }
            });
Ashkan

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

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