本文实例为大家分享了使用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);
您可能感兴趣的文章:android底部菜单栏实现原理与代码Android仿QQ空间底部菜单示例代码Android仿UC底部菜单栏实现原理与代码Android PopupWindow实现右侧、左侧和底部弹出菜单Android仿微信顶/底部菜单栏效果Android开发之微信底部菜单栏实现的几种方法汇总Android仿微信底部菜单栏功能显示未读消息数量Android使用Activity实现从底部弹出菜单或窗口的方法Android中底部菜单被输入法顶上去的解决方案Android仿网易严选底部弹出菜单效果