android - scrollview android 中 recyclerview 的滚动事件

标签 android scrollview android-recyclerview

我在单个 ScrollView 中有 2 个水平和 1 个垂直(网格布局)。

<ScrollView>

<Horizontal RecyclerView/>

<Horizontal RecyclerView/>

<Vertical RecyclerView/>

</ScrollView>

以上是我的观点的示 Intent 。

一旦垂直 recyclerviews 最后一项可见,我必须加载数据,但我无法获得垂直 View 的滚动事件。

这是我的 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. 首先为滚动监听器创建一个接口(interface)

    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 步中创建的接口(interface)。

  5. 将您的 Activity/Fragment 设置为 Scroll Listener,然后像这样使用:

    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 - scrollview android 中 recyclerview 的滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697498/

相关文章:

java.lang.ClassNotFoundException : Didn't find class "com.androidhive.jsonparsing.Search_medicine" 异常

ios - SwiftUI:导航链接在 ScrollView 中不起作用

qt - 在 ScrollView 中编辑 TextInput

Android FlowLayout 作为 RecyclerView LayoutManager

android - 如何在 RecyclerView 中隐藏 View

android - 从 android 相机拍照并将其发送到网络服务器

android - 来电未显示在锁定屏幕上

android - 在 AppWidget 的 RemoteViewsFactory 中的 onCreated() 之后依赖对 onDataSetChanged() 的调用是否安全

android - 是否可以在扩展通知中使用某种 ScrollView ?

android - CardView 是 wrap_content 列表项而不是 match_parent