在scrollview android中的recycleview的滚动事件



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

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

以上是我的观点的示意图。

我必须加载数据上一旦垂直回收视图的最后一项是可见的,但我无法得到我的垂直视图的滚动事件。

这是我的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;
                    }
                }
            });

但是这个setonscrollListener永远不会被触发。我如何得到上述情况下滚动事件。

提前感谢:)

我遇到的问题是如何解决的:

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

    public interface EndlessScrollListener {
        void onScrollChanged(EndlessScrollView scrollView,int x, int y, int oldx, int oldy);
    }
    
  2. 通过扩展ScrollView

    创建自定义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. 然后在你的Fragment/Activity布局中使用EndlessScrollView而不是默认的:

    <YOUR_PACKAGE_NAME.EndlessScrollView
        android:id="@+id/my_scroll_view">
        <!--  your recycler views ... -->
    </YOUR_PACKAGE_NAME.EndlessScrollView>
    
  4. 下一步你的Activity/Fragment应该实现你在步骤1中创建的接口

  5. 设置你的活动/片段为滚动监听器,然后像这样使用:

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

使用手势检测器获取RecyclerView中的滚动事件。

编辑 用NestedScrollView代替ScrollView解决了我的recyclerview滚动问题。

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

当你在彼此内部使用两个滚动元素时,你就陷入了困境!你应该计算回收器项目的高度,然后找到整个回收器的高度。请看下面的链接,我完整地解释了这个问题。

使用RecyclerView独立ScrollView和灵活的回收项目高度

希望对你有帮助

最新更新