【问题标题】:How to collapse the toolbar while I'm scrolling through a RecyclerView in ConstraintLayout?在 ConstraintLayout 中滚动 RecyclerView 时如何折叠工具栏?
【发布时间】:2020-01-17 01:05:17
【问题描述】:

我有一个活动,我设置了 NavigationDrawer 并且工作正常。这个活动承载了我使用过的片段:

setHasOptionsMenu(true);

启用菜单。我添加了一个 SearchView 并覆盖 onCreateOptionsMenu():

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    //Do some search stuff
}

一切正常。这是我的片段的布局文件:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ProgressBar
        android:id="@+id/progress_bar"
        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" />
</androidx.constraintlayout.widget.ConstraintLayout>

现在我想在滚动RecyclerView 时折叠工具栏。我知道我可以在我的布局文件中添加一个AppBarLayout 并在其中添加一个Toolbar,但是是否有可能在 ConstraintLayout 中实现与现有行为相同的行为?

【问题讨论】:

  • 动画不会那么流畅,你将重新发明轮子。最好只添加一个 AppBarLayout 和一个工具栏。此外,如果您要添加 appbarlayout,请考虑 CoordinatorLayout
  • @sonnet 谢谢你。我发现的所有示例都使用CoordinatorLayout。这是最好的方法吗?如果我仍然想通过ConstraintLayout 实现这一目标,我应该怎么做?
  • @Jorn AFAIK,CoordinatorLayout 是唯一具有您正在寻找的功能的本机视图。如果您想完全隐藏toolbar,则需要深入了解android开发。

标签: android android-recyclerview android-toolbar android-constraintlayout android-scroll


【解决方案1】:

CoordinatorLayout:

最好的办法是使用CoordinatorLayout。一个类似的问题here 应该能够帮助你。

既然您想通过ConstraintLayout 实现这一目标:

您必须为您的RecyclerView 创建一个OnScrollListener 类。该类将有一个 onScrolled() 方法,该方法将具有参数 - dx、dy、水平和垂直滚动的数量。 Here 是一个关于如何跟进的链接。

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    您可以在 CordinaterLayout 的帮助下完成此操作。请查看此答案。

    Hide AppBar when scrolling down

    【讨论】:

    • 谢谢,但我的问题与ConstraintLayout 有关。我可以用它实现同样的目标吗?如果是,怎么做?
    • 是的,您可以使用 ConstraintLayout 实现相同的效果。 1:添加ConstraintLayout的CordinateLayout的ParentLayout 2:使用也可以使用android.support.design.widget.CollapsingToolbarLayout 3:启用rescyclerView的nestedscroll视图。
    • 这可能是一条评论。请不要只是为了发布另一个现有解决方案的链接而回答
    • @umerfarooq 我不想在.XML 文件中使用带有`Toolbar` 的AppBarLayout,我只想使用现有的。有可能吗?
    【解决方案3】:
     <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <androidx.core.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
     </androidx.core.widget.NestedScrollView>
    
     <ProgressBar
        android:id="@+id/progress_bar"
        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" />
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    在片段中使用此布局(在 Nestedscrollview 中包装 Recyclerview)。

    在主布局中,在 Coordinator 布局中使用 AppbarLyaout 像这样

      <androidx.coordinatorlayout.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:id="@+id/DashCoor"
            android:fitsSystemWindows="true"
            tools:context=".Activities.DashboardActivity">
    
    
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
    
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:visibility="gone"
                    app:expandedTitleMarginStart="48dp"
                    app:expandedTitleMarginEnd="64dp"
                    app:layout_scrollFlags="scroll|enterAlways|snap"
                    >
                    ​
    
    
                   //Layout to be collpased
    
                </com.google.android.material.appbar.CollapsingToolbarLayout>
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:titleTextColor="@color/white"
                    android:fitsSystemWindows="true"
                    android:minHeight="?attr/actionBarSize"
                    app:theme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_scrollFlags="scroll|enterAlways|snap"
                    />
    
             </com.google.android.material.appbar.AppBarLayout>
    
             </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    希望你解决它:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 2017-09-27
      相关资源
      最近更新 更多