android - Recyclerview 滚动在嵌套 ScrollView 中的 fragment 中不起作用

标签 android android-recyclerview fragment android-coordinatorlayout nestedscrollview

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

这是我对滚动代码的总结

  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")

这是我的 Activity 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>

这是我的 fragment 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>

在我设置的fragment的onActivtyCreated

scrollView.isNestedScrollingEnabled = false

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

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

最佳答案

设置

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

关于android - Recyclerview 滚动在嵌套 ScrollView 中的 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52875242/

相关文章:

javascript - react native 电话号码输入

android - key 轮换后,我是否仍然需要旧 key 来签署应用程序的更新?

java - 如何在 Android 应用程序中一次更改所有 Activity 转换?

android - RecyclerView 的滚动效果

java - 安卓 fragment : fragments inside a Frame or other layouts?

java - fragment 中的函数使模拟器崩溃

android - 使用 TObject 后代调用 std::vector::push_back 时出现 C++ Builder bccarm 错误

java - 如何检索 RecyclerView 位置

android - 当我们进入 ListView 的开头时,回收器 View 显示按钮可见

java - 处理 Android fragment 事务问题的正确/最佳方法