【发布时间】: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