首页 > 解决方案 > Recyclerview 滚动在嵌套滚动视图中的片段中不起作用

问题描述

首先:我之前使用回收器视图和滚动监听器工作,一切都很好,但这是第一次使用协调器布局并在嵌套滚动视图中使用片段,所以我认为问题与设计有关

这是我对滚动代码的总结

  categoryRecyclerView?.addOnScrollListener(object : RecyclerView.OnScrollListener() {

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            Log.e("scrolling","scroll")
            if (dy > 0) {
                Log.e("scrolling","vertical")

因此,当我滚动时,我在 logcat 中没有看到 ("scrolling","vertical"),但无论我是否滚动,我都只看到一次 ("scrolling","scroll")

这是我的活动 xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/layoutBackground">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:padding="8dp"
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        android:padding="8dp"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:itemIconTint="@drawable/navigation_item_selector"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation_menu" />

</android.support.design.widget.CoordinatorLayout>

这是我的片段 xml

<?xml version="1.0" encoding="utf-8"?>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/layoutBackground"
        tools:context=".fragments.AllItemsFragment">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/categoryRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/entertainment_recycler_view_item" />

        <ProgressBar
            android:id="@+id/loadingCategoryList"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

在我设置的片段中的 onActivtyCreated

scrollView.isNestedScrollingEnabled = false

在初始化 recyclerview 代码中,我将 recyclerview 设置为

categoryRecyclerView?.isNestedScrollingEnabled = false
categoryRecyclerView?.setHasFixedSize(false);

标签: androidandroid-recyclerviewfragmentandroid-coordinatorlayoutnestedscrollview

解决方案


categoryRecyclerView?.isNestedScrollingEnabled = true

而不是使用recyclerview.addOnScrollListener(),而是使用nestedScrollView.setOnScrollChangeListener()。

    nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX,
      int oldScrollY) {
    LinearLayoutManager layoutManager = (LinearLayoutManager) questionRecyclerView.getLayoutManager();

    if (layoutManager != null) {
      int visibleItemCount = layoutManager.getChildCount();
      int totalItemCount = layoutManager.getItemCount();
      int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

      if (!questionAdapter.isLoading()) {
        if (visibleItemCount +firstVisibleItemPosition >= totalItemCount
            && firstVisibleItemPosition >= 0
            && totalItemCount >= PAGE_SIZE) {

             // loadMore();  
            }
       }
      }
    }
}});

推荐阅读