공부방/Android

Android BottomNavigation View Behavior CustomView Floating (하단 네비게이션 뷰 위에 있는 커스텀뷰 구현하기)

soycrab 2020. 2. 9. 18:14

 

 

BottomNavigation 이 NestedScorll 에 의해 위아래로 움직이거나 해도 

항상 BottomNavigation 바로 위에 떠있는 CustomView가 필요할때가 있다. 

아래는 예시 XML 이다. 

 

<?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/activity_main_crdl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/activity_main_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="15dp"
            android:paddingLeft="15dp"
            app:layout_scrollFlags="scroll|enterAlways"/>
            
    </com.google.android.material.appbar.AppBarLayout>

    <include
        android:id="@+id/content_main"
        layout="@layout/content_main" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/act_main_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:labelVisibilityMode="labeled"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:layout_insetEdge="bottom"
        app:menu="@menu/main_bottom_nav_menu" />

    <include layout="@layout/view_custom"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="bottom"
        app:layout_dodgeInsetEdges="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>


 

 

위의 XML 처럼 설정하면 굳이 Behavior Custom 클래스 없이 

항상 BottomNavigation 위에 떠있는 CustomView 를 구현할수 있다. 

Scroll에따라 BottomNavigation 이 snap 을 해도 

CustomView 는 항상 BottomNavigation 위에 떠있다. 

 

아래가 핵심 코드이다. 

app:layout_insetEdge="bottom"

떠있을 대상이 되는 뷰에 위의 코드를 넣어주고 

 

app:layout_dodgeInsetEdges="bottom" 

떠있어야 하는 뷰에 아래 옵션을 붙여주면 구현이 가능하다. 

반응형