android - 在 ConstraintLayout 中滚动浏览 RecyclerView 时如何折叠工具栏?

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

我有一个 Activity ,我设置了 NavigationDrawer 并且工作正常。此 Activity 承载了我使用过的 fragment :

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
}

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

<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 中现有行为相同的行为?

最佳答案

使用CoordinatorLayout:

最好的办法是使用 CoordinatorLayout。类似问题here应该可以帮助你。

因为你想用 ConstraintLayout 来实现:

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

希望对您有所帮助:)

关于android - 在 ConstraintLayout 中滚动浏览 RecyclerView 时如何折叠工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57952688/

相关文章:

kotlin - 如何设置 RecyclerView 中的属性在 ViewModel 列表项属性更新时更新?

android - 如何在工具栏的右侧放置 ProgressBar?

java - 为什么 startActivityForResult() 方法在这种情况下不起作用

安卓模拟器 : Easy way to simulate a process restart due to low memory?

android - 在水平和垂直回收 View 中滚动问题

android - 放大 RecyclerView 中的项目以重叠 2 个相邻的项目 Android

android - 如何使用支持操作栏将默认主页显示为向上指示器?

android - 如何在 Android 中更改工具栏标题的颜色?

android - 使用数据绑定(bind)库将基本类型导入布局

java - Android - 显示所有已安装应用程序的应用程序选择器