[깡샘의 안드로이드 프로그래밍] 정리 20 - persistent bottom sheet
본 글은 [깡샘의 안드로이드 프로그래밍 - 루비페이퍼] 의 내용을 발췌한 것입니다.
좀더 자세한 내용은 책을 통해 확인해 주세요.

18장. 머티리얼 디자인
18.4.1. persistent bottom sheet
persistent bottom sheet를 작성하려면 액티비티의 레이아웃 XML 파일에 persistent bottom sheet를 위한 뷰를 추가해 줍니다.
<android.support.design.widget.CoordinatorLayout ... >
<!-- Activity Main Content View-->
<LinearLayout ... >
...
</LinearLayout>
<!-- Bottom Sheet View-->
<LinearLayout ...
android:layout_height="250dp"
app:behavior_peekHeight="120dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<!-- 중략-->
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Bottom Sheet를 이용하려면 전체 내용을 CoordinatorLayout으로 감싸야 하며, 액티비티의 메인 콘텐츠를 표현하는 뷰와 Bottom Sheet를 위한 뷰를 하위에 추가해야 합니다.
Bottom Sheet를 위한 뷰는 layout_behavior 속성이 중요합니다. 이 속성값을 android.support.design.widget.BottomSheetBehavior로 지정하면 BottomSheetBehavior 클래스가 특별하게 처리하므로 bottom sheet로 동작합니다.
- layout_height: bottom sheet의 최대 스크롤 크기
- behavior_peekHeight: bottom sheet의 초기 크기, 최소 스크롤 크기
- behavior_hideable: 사용자 스크롤에 의해 사라질 것인지 설정
bottom sheet 화면이 뜨면 초기 behavior_peekHeight에 지정한 크기만큼 화면에 보이고, 이후 사용자가 스크롤하여 bottom sheet의 크기가 커질 수 있습니다. 최대 layout_height에서 지정한 크기만큼 커지며, 다시 아래로 스크롤 하여 줄어들 때 behavior_peekHeight에 지정한 크기만큼 줄어듭니다. 그리고 behavior_hideable을 true로 지정하면 스크롤에 의해 화면에서 사라집니다.
//persistentBottomSheet로 사용할 view 획득
View bottomSheet = coordinatorLayout.findViewById(R.id.lab4_bottom_sheet);
//획득한 view를 bottomsheet로 지정
persistentBottomSheet = BottomSheetBehavior.from(bottomSheet);
레이아웃 XML 파일에 정의한 뷰를 BottomSheetBehavior.from( ) 함수로 지정해 주면 bottom sheet가 됩니다.
사용자의 bottom sheet 이벤트 처리도 가능합니다.
persistentBottomSheet.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
BottomSheetCallback을 구현한 이벤트 핸들러를 setBottomSheetCallback ( ) 함수로 등록하여 이벤트를 처리합니다.
bottom sheet의 상태값이 변경될 때 onStateChanged( ) 함수가 호출되며, 사용자가 bottom sheet를 스크롤 할 때 onSlide ( ) 함수가 호출됩니다. onSlide ( ) 함수의 매개변수로 스크롤 정보가 전달되며, onStateChanged ( ) 함수로 bottom sheet의 상태값이 전달됩니다.
상태값으로는 STATE_DRAGGING, STATE_SETTLING, STATE_EXPANDED, STATE_COLLAPSED, STATE_HIDDEN이 이용됩니다.
자바 코드에서 bottom sheet의 상태값을 변경할 수도 있습니다. 예를 들어, bottom sheet를 레이아웃 XML에 정의할 때 behavior_hideable 속성을 true로 지정하면 사용자에 의해 bottom sheet가 라집니다. 그리고 다시 사용자 스크롤로는 나타나지 않습니다. 다시 나타나게 하려면 setState ( ) 함수로 설정할 수 있습니다.
persistentBottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
