Recyclerview滚动不适用于嵌套滚动视图中的片段

时间:2018-10-18 13:31:03

标签: android android-recyclerview fragment android-coordinatorlayout nestedscrollview

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

这是我的滚动代码摘要

  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中看到(“滚动”,“垂直”),但是我只会看到一次(“滚动”,“滚动”)

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

在onActivty中创建在我设置的片段中

scrollView.isNestedScrollingEnabled = false

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

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

1 个答案:

答案 0 :(得分:0)

设置

  

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