Hide/Show bottomNavigationView 滚动
Hide/Show bottomNavigationView on Scroll
我必须在向上滚动时隐藏底部导航视图并在向下滚动时显示。如何实现?
我的布局是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@+id/navigation"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp">
<FrameLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:menu="@menu/dashboard_slider_menu" />
</RelativeLayout>
我附上了视图的屏幕截图。请查收。
使用这个
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0 && csButtonLay.isShown())
{
bottomBar.setVisibility(View.GONE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
bottomBar.setVisibility(View.VISIBLE);
}
super.onScrollStateChanged(recyclerView, newState);
}
});
更新
只需将一个属性添加到 BottomNavigationView
Material 库 AndroidX
<com.google.android.material.bottomnavigation.BottomNavigationView
....
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/>
支持库版本28.0.0
或higher version
<android.support.design.widget.BottomNavigationView
....
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
Note:- Your XML should follow the structure of XML given below in old answer.
**旧答案(仍然有效)**
您需要一个帮手 class 来完成此操作。此解决方案的工作方式类似于 Google Material Design Guideline.
创建 class BottomNavigationViewBehavior
public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
private int height;
@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
height = child.getHeight();
return super.onLayoutChild(parent, child, layoutDirection);
}
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
BottomNavigationView child, @NonNull
View directTargetChild, @NonNull View target,
int axes, int type)
{
return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed,
@ViewCompat.NestedScrollType int type)
{
if (dyConsumed > 0) {
slideDown(child);
} else if (dyConsumed < 0) {
slideUp(child);
}
}
private void slideUp(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(0).setDuration(200);
}
private void slideDown(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(height).setDuration(200);
}
}
要使用此行为,您需要使用协调器布局...
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kliff.digitaldwarka.activity.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/myAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<!---your RecyclerView/Fragment Container Layout-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemBackground="@color/white"
app:menu="@menu/bottom_nav_menu" />
</android.support.design.widget.CoordinatorLayout>
<!---NavigationView-->
</android.support.v4.widget.DrawerLayout>
将此代码添加到包含底部导航的 Activity..
mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());
试试这个,
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0 && bottom_navigation.isShown()) {
bottom_navigation.setVisibility(View.GONE);
} else if (dy < 0 ) {
bottom_navigation.setVisibility(View.VISIBLE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
向上滚动时的图像:-
向下滚动时的图像:
更新答案在最新库更新后:
滚动时隐藏 BottomNavigationView
现在只需布局中的一个标志即可!从版本 28.0.0-alpha1
或 material/androidX 1.0.0-alpha1
开始。
我使用后一种方法更新了我的项目,因为该版本现在是一个稳定的候选版本。 更新:使用完全发布的版本"1.0.0"
!
开箱即用的新行为称为 HideBottomViewOnScrollBehavior
。在 BottomNavigationView
上将其设置为
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
如最新docs所述。
这是一个完整的例子:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
android:layout_gravity="bottom"
app:layout_insetEdge="bottom"
app:menu="@menu/navigation" />
与在滚动时隐藏工具栏一样,您必须确保内容是 class 支持最新的滚动,例如 RecyclerView
和 NestedScrollView
.
这可确保一切正常,如设计规范animation中所示
PS:labelVisibilityMode
是另一个很酷的附加功能,您可以免费获得它,因为它不厌其烦地进行更新,并且在 design specs.
中有深入的描述。
- 将您的项目更新到 Androidx,即 重构 >> 迁移到 androidx(最低 Android 工作室版本 3.4)
- 使用默认的底部导航菜单 xml 文件,将父 约束布局替换为协调器布局 。
- 添加行 app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
即
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".dashboards.Admin_dashboard_main">
<include layout="@layout/toolbar" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="0dp"
android:padding="0dp">
<!-- Fragments Container -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="MainActivity"
tools:showIn="@layout/activity_tenant_dashboard"
android:id="@+id/fragment_container">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- Bottom Navigation View -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:layout_gravity="bottom"
app:menu="@menu/menu_admin_dashboard_main"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
只需使用 CoordinatorLayout
作为 parent 容器并添加 app:layout_behavior
在 child View
中设置行为 @string/hide_bottom_view_on_scroll_behavior
这就是解决方案。
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".Main2Activity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_above="@id/nav_view"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
编码愉快。
只需将此添加到您的 xml
<BottomNavigationView
....
....
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
这可以帮助某人
阅读更多:https://material.io/develop/android/components/app-bars-bottom
添加
app:hideOnScroll="真"
在 BottomAppBar 内,如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout
...>
...
<com.google.android.material.bottomappbar.BottomAppBar
...
app:hideOnScroll="true"
/>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我在使用 Recyclerview
时遇到了这个问题。
属性:
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
对我来说只有部分效果,所以我不得不实施另一个解决方案。
我在 MainActivity
中定义了 BottomNavigationView
,所以我必须设置几个方法在滚动过程中为其设置动画。
class MainActivity : AppCompatActivity() {
private var animator: ObjectAnimator? = null
.
.
.
fun slideDown() {
nav_view?.let {
if (animator == null && it.translationY == 0f) {
animator = translationObjectY(it, 0f, it.height.toFloat() + it.marginBottom.toFloat()).apply {
doOnEnd {
animator = null
}
}
}
}
}
fun slideUp() {
nav_view?.let {
if (animator == null && it.translationY == it.height.toFloat() + it.marginBottom.toFloat()) {
animator = translationObjectY(it, it.height.toFloat() + it.marginBottom.toFloat(), 0f).apply {
doOnEnd {
animator = null
}
}
}
}
}
}
translationObjectY
是扩展函数:
fun translationObjectY(
targetView: View?,
startY: Float,
endY: Float,
duration: Long = 200L
) : ObjectAnimator {
return ObjectAnimator.ofFloat(targetView, "translationY", startY, endY).apply {
this.duration = duration
interpolator = LinearOutSlowInInterpolator()
start()
}
}
我终于创建了一个自定义 Recyclerview
:
class CustomRecyclerView(
context: Context,
attrs: AttributeSet?,
defStyle: Int,
) : RecyclerView(context, attrs, defStyle) {
constructor(context: Context)
: this(context, null, 0)
constructor(context: Context, attrs: AttributeSet)
: this(context, attrs, 0)
init {
this.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0) {
// Scrolling up
hideBottomMenu()
} else {
// Scrolling down
showBottomMenu()
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
}
})
}
private fun hideBottomMenu() {
(context as? MainActivity)?.slideDown()
}
private fun showBottomMenu() {
(context as? MainActivity)?.slideUp()
}
}
然后您可以像这样在片段中实现它:
<com.studio.mattiaferigutti.kamasutra.custom.CustomRecyclerView
android:id="@+id/searchRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我必须在向上滚动时隐藏底部导航视图并在向下滚动时显示。如何实现? 我的布局是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@+id/navigation"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp">
<FrameLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:menu="@menu/dashboard_slider_menu" />
</RelativeLayout>
我附上了视图的屏幕截图。请查收。
使用这个
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0 && csButtonLay.isShown())
{
bottomBar.setVisibility(View.GONE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
bottomBar.setVisibility(View.VISIBLE);
}
super.onScrollStateChanged(recyclerView, newState);
}
});
更新
只需将一个属性添加到 BottomNavigationView
Material 库 AndroidX
<com.google.android.material.bottomnavigation.BottomNavigationView
....
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/>
支持库版本28.0.0
或higher version
<android.support.design.widget.BottomNavigationView
....
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
Note:- Your XML should follow the structure of XML given below in old answer.
**旧答案(仍然有效)**
您需要一个帮手 class 来完成此操作。此解决方案的工作方式类似于 Google Material Design Guideline.
创建 class BottomNavigationViewBehavior
public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
private int height;
@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
height = child.getHeight();
return super.onLayoutChild(parent, child, layoutDirection);
}
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
BottomNavigationView child, @NonNull
View directTargetChild, @NonNull View target,
int axes, int type)
{
return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed,
@ViewCompat.NestedScrollType int type)
{
if (dyConsumed > 0) {
slideDown(child);
} else if (dyConsumed < 0) {
slideUp(child);
}
}
private void slideUp(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(0).setDuration(200);
}
private void slideDown(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(height).setDuration(200);
}
}
要使用此行为,您需要使用协调器布局...
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kliff.digitaldwarka.activity.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/myAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<!---your RecyclerView/Fragment Container Layout-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemBackground="@color/white"
app:menu="@menu/bottom_nav_menu" />
</android.support.design.widget.CoordinatorLayout>
<!---NavigationView-->
</android.support.v4.widget.DrawerLayout>
将此代码添加到包含底部导航的 Activity..
mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());
试试这个,
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0 && bottom_navigation.isShown()) {
bottom_navigation.setVisibility(View.GONE);
} else if (dy < 0 ) {
bottom_navigation.setVisibility(View.VISIBLE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
向上滚动时的图像:-
向下滚动时的图像:
更新答案在最新库更新后:
滚动时隐藏 BottomNavigationView
现在只需布局中的一个标志即可!从版本 28.0.0-alpha1
或 material/androidX 1.0.0-alpha1
开始。
我使用后一种方法更新了我的项目,因为该版本现在是一个稳定的候选版本。 更新:使用完全发布的版本"1.0.0"
!
开箱即用的新行为称为 HideBottomViewOnScrollBehavior
。在 BottomNavigationView
上将其设置为
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
如最新docs所述。
这是一个完整的例子:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
android:layout_gravity="bottom"
app:layout_insetEdge="bottom"
app:menu="@menu/navigation" />
与在滚动时隐藏工具栏一样,您必须确保内容是 class 支持最新的滚动,例如 RecyclerView
和 NestedScrollView
.
这可确保一切正常,如设计规范animation中所示
PS:labelVisibilityMode
是另一个很酷的附加功能,您可以免费获得它,因为它不厌其烦地进行更新,并且在 design specs.
- 将您的项目更新到 Androidx,即 重构 >> 迁移到 androidx(最低 Android 工作室版本 3.4)
- 使用默认的底部导航菜单 xml 文件,将父 约束布局替换为协调器布局 。
- 添加行 app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
即
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".dashboards.Admin_dashboard_main">
<include layout="@layout/toolbar" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="0dp"
android:padding="0dp">
<!-- Fragments Container -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="MainActivity"
tools:showIn="@layout/activity_tenant_dashboard"
android:id="@+id/fragment_container">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- Bottom Navigation View -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:layout_gravity="bottom"
app:menu="@menu/menu_admin_dashboard_main"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
只需使用 CoordinatorLayout
作为 parent 容器并添加 app:layout_behavior
在 child View
中设置行为 @string/hide_bottom_view_on_scroll_behavior
这就是解决方案。
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".Main2Activity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_above="@id/nav_view"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
编码愉快。
只需将此添加到您的 xml
<BottomNavigationView
....
....
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
这可以帮助某人 阅读更多:https://material.io/develop/android/components/app-bars-bottom
添加 app:hideOnScroll="真"
在 BottomAppBar 内,如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout
...>
...
<com.google.android.material.bottomappbar.BottomAppBar
...
app:hideOnScroll="true"
/>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我在使用 Recyclerview
时遇到了这个问题。
属性:
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
对我来说只有部分效果,所以我不得不实施另一个解决方案。
我在 MainActivity
中定义了 BottomNavigationView
,所以我必须设置几个方法在滚动过程中为其设置动画。
class MainActivity : AppCompatActivity() {
private var animator: ObjectAnimator? = null
.
.
.
fun slideDown() {
nav_view?.let {
if (animator == null && it.translationY == 0f) {
animator = translationObjectY(it, 0f, it.height.toFloat() + it.marginBottom.toFloat()).apply {
doOnEnd {
animator = null
}
}
}
}
}
fun slideUp() {
nav_view?.let {
if (animator == null && it.translationY == it.height.toFloat() + it.marginBottom.toFloat()) {
animator = translationObjectY(it, it.height.toFloat() + it.marginBottom.toFloat(), 0f).apply {
doOnEnd {
animator = null
}
}
}
}
}
}
translationObjectY
是扩展函数:
fun translationObjectY(
targetView: View?,
startY: Float,
endY: Float,
duration: Long = 200L
) : ObjectAnimator {
return ObjectAnimator.ofFloat(targetView, "translationY", startY, endY).apply {
this.duration = duration
interpolator = LinearOutSlowInInterpolator()
start()
}
}
我终于创建了一个自定义 Recyclerview
:
class CustomRecyclerView(
context: Context,
attrs: AttributeSet?,
defStyle: Int,
) : RecyclerView(context, attrs, defStyle) {
constructor(context: Context)
: this(context, null, 0)
constructor(context: Context, attrs: AttributeSet)
: this(context, attrs, 0)
init {
this.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0) {
// Scrolling up
hideBottomMenu()
} else {
// Scrolling down
showBottomMenu()
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
}
})
}
private fun hideBottomMenu() {
(context as? MainActivity)?.slideDown()
}
private fun showBottomMenu() {
(context as? MainActivity)?.slideUp()
}
}
然后您可以像这样在片段中实现它:
<com.studio.mattiaferigutti.kamasutra.custom.CustomRecyclerView
android:id="@+id/searchRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />