【问题标题】:How to scroll a Recyclerview which is inside multi-level nested fragment and Viewpager?如何滚动多级嵌套片段和 Viewpager 内的 Recyclerview?
【发布时间】:2019-01-28 14:31:42
【问题描述】:

当我向上滚动 recyclerview 时,我试图隐藏工具栏。此回收站视图位于 Fragment(名为 Fragment 2)内。该片段在带有 3 个选项卡的 Viewpager 中重新使用(重新使用相同的片段),该片段位于 Fragment(named Fragment 1) 中。现在这个 Fragment 1 被加载到一个 Framelayout 中,该 Framelayout 托管在一个 Activity 中

编辑 1: 在这种情况下,即使是回收站视图也不会滚动。最后的项目永远不会出现。我无法滚动到最后一项的末尾

我尝试使用协调器布局和嵌套滚动视图,但没有给出任何结果。

活动主要

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/MainBG"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".iccrankings.ICCRankingNew">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">


        </android.support.v7.widget.Toolbar>


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="?attr/cardBGC">


            <Spinner
                android:id="@+id/spinner_icc_categories"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:dropDownWidth="match_parent"
                android:overlapAnchor="false"
                android:spinnerMode="dropdown"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            </Spinner>


        </android.support.constraint.ConstraintLayout>

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


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <FrameLayout
            android:id="@+id/main_container_of_ranks_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />


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


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

片段 1

<?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"
    tools:context=".iccrankings.fragments.RankViewPagerContainerFragment">




    <android.support.design.widget.TabLayout
        android:id="@+id/tabs_all_matches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabBackground="@drawable/tab_color_selector"
        app:tabTextColor="@color/colorAccent">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/odi_tab" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test_tab" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/t20_tab" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/categoryDataContainerPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabs_all_matches" />
</android.support.constraint.ConstraintLayout>

片段 2

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    tools:context=".iccrankings.fragments.IccHumanFragment">



        <android.support.v7.widget.RecyclerView
            android:id="@+id/icc_human_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </android.support.v7.widget.RecyclerView>






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

我是attaching an image which illustrates the nested views and desired results,它描述了视图的组成和期望的结果。

提前致谢

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要在CollapsingToolbarLayout 中添加您的活动工具栏,并为滚动行为设置所需的标志。

    查看此页面了解更多详情:

    https://guides.codepath.com/android/handling-scrolls-with-coordinatorlayout

    【讨论】:

      【解决方案2】:

      你应该在你的 AppBarLayout 中使用CollapsingToolbarLayout

      应该是这样的:

      <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="@dimen/appbar_padding_top"
      android:theme="@style/AppTheme.AppBarOverlay"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent">
      
          <android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/collapsing_toolbar"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              app:contentScrim="@android:color/transparent"
              app:layout_scrollFlags="scroll|snap">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:layout_weight="1"
                  android:background="?attr/colorPrimary"
                  app:layout_scrollFlags="scroll|enterAlways"
                  app:popupTheme="@style/AppTheme.PopupOverlay"
                  app:title="@string/app_name">
      
      
              </android.support.v7.widget.Toolbar>
      
      
              <android.support.constraint.ConstraintLayout
                  android:layout_width="match_parent"
                  android:layout_height="60dp"
                  android:background="?attr/cardBGC">
      
      
                  <Spinner
                      android:id="@+id/spinner_icc_categories"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="8dp"
                      android:layout_marginBottom="8dp"
                      android:dropDownWidth="match_parent"
                      android:overlapAnchor="false"
                      android:spinnerMode="dropdown"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toTopOf="parent">
      
                  </Spinner>
      
      
              </android.support.constraint.ConstraintLayout>
          </android.support.design.widget.CollapsingToolbarLayout>
      </android.support.design.widget.AppBarLayout>
      

      【讨论】:

      • 这总是隐藏工具栏
      • 还有。我更新了问题。我的 Recyclerview 没有滚动到最后一个项目
      • @abk007 这可能不起作用,因为您使用 CoordinatorLayout 作为 RecyclerView 的容器。你也应该将 layout_behavior 设置为 RecyclerView。
      • 在这里我从工作项目中发布了我的布局文件。看看这些,也许你会发现你的布局有什么问题。要点:gist.github.com/khojiakbar17/c95b80697caca253bee4eceb52ed3014
      【解决方案3】:

      你可以在没有Collapsing Toolbar 的情况下实现这一点,例如,如果你使用下面的一些代码向下滚动,你可以隐藏你的工具栏:

      <android.support.design.widget.CoordinatorLayout 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="#ddd"
          tools:context=".ControllerAdding.ControllerPickerActivity"
          >
      
          <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay"
              android:fitsSystemWindows="true">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="#df1b39"
                  app:contentInsetStart="0dp"
                  app:titleTextColor="@color/pureWhite"
                  style="@style/RobotoBoldTextAppearance"
                  app:layout_scrollFlags="scroll|enterAlways">
      
                  <TextView
                      android:id="@+id/textView6"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:gravity="center"
                      android:text="Products List"/>
              </android.support.v7.widget.Toolbar>
      
          </android.support.design.widget.AppBarLayout>
      

      请务必在您的工具栏中添加app:layout_scrollFlags="scroll|enterAlways",因为如果您不这样做,然后在此处添加 RecyclerView,则该 Recycler 将无法滚动。

      这是一个经过测试的代码,我用于我自己的应用程序,所以如果你看到一些样式或......因为它。

      【讨论】:

        猜你喜欢
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-23
        相关资源
        最近更新 更多