Android使用CoordinatorLayout实现底部弹出菜单
本文实例为大家分享了使用CoordinatorLayout实现底部弹出菜单的具体代码,供大家参考,具体内容如下
第一步:添加依赖:
compile "com.android.support:design:${project.properties.get("support")}"
第二步:布局引用:
<android.support.design.widget.CoordinatorLayout 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" android:background="#04827c"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> //页面布局 </RelativeLayout> <include //引入菜单布局 android:id="@+id/au_bottom_sheet" layout="@layout/view_audio_list" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" app:behavior_hideable="true" app:behavior_peekHeight="0dp" app:layout_behavior="@string/bottom_sheet_behavior" /> </android.support.design.widget.CoordinatorLayout>
第三步:使用:
//找到BottomSheetBehavior BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.au_bottom_sheet)); //设置弹出高度 behavior.setPeekHeight(WindowHeight / 2); //默认隐藏 behavior.setState(BottomSheetBehavior.STATE_HIDDEN); //添加消失监听 behavior.setBottomSheetCallback(bottomSheetCallback);
添加菜单消失监听:
//状态监听,通过这个监听菜单是否消失 private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { if (newState == BottomSheetBehavior.STATE_HIDDEN) { if (!isBehaviorShowing(behavior)) { //菜单已经消失 } } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { //滑动监听 } };
判断是否可见:
private Boolean isBehaviorShowing(BottomSheetBehavior behavior) { return behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED || behavior.getState() == BottomSheetBehavior.STATE_EXPANDED || behavior.getState() == BottomSheetBehavior.STATE_SETTLING; }
收回菜单:
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
弹出菜单:
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)