Kar*_*nga 42 android android-layout android-fragments bottomnavigationview
我必须在向上滚动时隐藏底部导航视图并在向下滚动时显示.如何实现这一点?我的布局是这样的
<?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>
Run Code Online (Sandbox Code Playgroud)
我附上了视图的截图.请检查一下.
Abh*_*ngh 92
UPDATE
更新到最新的支持库版本28.0.0
,只需添加一个属性即可higher version
.
<BottomNavigationView
....
....
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
Run Code Online (Sandbox Code Playgroud)
注意: - 您的XML应遵循旧答案中下面给出的XML结构.
旧答案(仍然有效)
您需要一个辅助类来执行此操作.此解决方案的工作方式与Google Material Design Guide相同.
创建一个类 BottomNavigationView
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);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用此行为,您需要使用cooradinator布局...
<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>
Run Code Online (Sandbox Code Playgroud)
将此代码添加到包含底部导航的Activity中.
mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());
Run Code Online (Sandbox Code Playgroud)
小智 12
试试这个,
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);
}
});
Run Code Online (Sandbox Code Playgroud)
向上滚动图像: -
向下滚动图像:
最新库更新后更新的答案:
BottomNavigationView
现在只需在布局中使用一个标志即可隐藏滚动!从版本28.0.0-alpha1
或材质/ androidX开始1.0.0-alpha1
.
我使用后一种方法更新了我的项目,因为该版本现在是一个稳定的候选版本.更新:使用完全发布的版本"1.0.0"
!
调用新的开箱即用行为HideBottomViewOnScrollBehavior
.将其设置BottomNavigationView
为
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
最新文档中所述.
这是一个完整的例子:
<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" />
Run Code Online (Sandbox Code Playgroud)
由于与工具栏上滚动的隐藏,你必须确保内容是,支持最新的滚动类像RecyclerView
和NestedScrollView
.
这可确保所有工作正如设计规格中的动画所示
PS:这labelVisibilityMode
是另一个很酷的补充,你可以免费获得更新的麻烦,这在设计规范中有详细描述.
即
<?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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
52144 次 |
最近记录: |