Android中ScrollView内RecyclerView的滚动事件

3

我在一个滚动视图中使用了2个水平的和1个垂直的网格布局(Grid layout)。

<ScrollView>

<Horizontal RecyclerView/>

<Horizontal RecyclerView/>

<Vertical RecyclerView/>

</ScrollView>

以下是我的视图示意图。
一旦垂直recyclerview的最后一个项目可见,我就需要加载数据,但是我无法获取垂直视图的滚动事件。
以下是我的滚动监听器。
             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从未被触发。我该如何获得上述情况的滚动事件呢?
提前感谢:)

我不认为在可滚动视图中嵌套可滚动视图(例如ScrollView中的RecyclerView)是最好的选择,尽管这在API 21以后得到了支持。但无论如何,请看一下这个链接,它可能会有所帮助:https://dev59.com/Yuo6XIcBkEYKwwoYSCpJ?rq=1 - Daniel Ocampo
在一个滚动视图中使用两个可滚动视图有点困难,当你想要它们具有灵活性时,这变得更加困难。 - Ashkan
3个回答

6
我有一个问题,这是如何解决它的方法:
  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
    
            }
        }
    }
    

1
太棒了,它确实帮了我很多。我一定会推荐这个的。谢谢啊。 - Artist404

2
使用手势检测器来获取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
                            }
                    }
            });

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接