android滚动视图里面的recyclerview的滚动事件 [英] scroll event for recyclerview inside scrollview android

查看:305
本文介绍了android滚动视图里面的recyclerview的滚动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Scrollview中有2个水平和1个垂直(网格布局).

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.

一旦垂直recyclerviews的最后一项可见,我就必须加载数据,但是我无法获取垂直视图的滚动事件.

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.

这是我的滚动浏览器.

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

但是此setonscrollListener永远不会被触发.如何获得上述情况的滚动事件.

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

提前感谢:)

推荐答案

我在这里遇到的问题是如何解决它:

I had the problem here is how to solve it:

  1. 首先为滚动侦听器创建一个接口

  1. First create an Interface for scroll listener

public interface EndlessScrollListener {
    void onScrollChanged(EndlessScrollView scrollView,int x, int y, int oldx, int oldy);
}

  • 通过扩展ScrollView创建自定义ScrollView

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

  • 然后在片段/活动"布局中使用EndlessScrollView而不是默认":

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

  • 接下来,您的活动/片段"应该实现您在步骤1中创建的接口.

  • Next your Activity/Fragment should implement the Interface your created in step 1.

    将活动/片段"设置为滚动侦听器",然后像这样使用:

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

  • 这篇关于android滚动视图里面的recyclerview的滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆