Android uses CoordinatorLayout to implement the bottom pop up menu
- 2021-10-24 23:36:57
- OfStack
This article example for everyone to share the use of CoordinatorLayout to achieve the bottom of the pop-up menu specific code, for your reference, the specific content is as follows
Step 1: Add dependencies:
compile "com.android.support:design:${project.properties.get("support")}"
Step 2: Layout references:
<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">
// Page layout
</RelativeLayout>
<include // Introducing menu layout
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>
Step 3: Use:
// Find BottomSheetBehavior
BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.au_bottom_sheet));
// Set ejection height
behavior.setPeekHeight(WindowHeight / 2);
// Default hide
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
// Add disappearing monitor
behavior.setBottomSheetCallback(bottomSheetCallback);
Add menu disappearing monitor:
// Status monitoring, whether it disappears through this monitoring menu
private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
if (!isBehaviorShowing(behavior)) {
// The menu has disappeared
}
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// Sliding monitoring
}
};
Determine whether it is visible:
private Boolean isBehaviorShowing(BottomSheetBehavior behavior) {
return behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED
|| behavior.getState() == BottomSheetBehavior.STATE_EXPANDED
|| behavior.getState() == BottomSheetBehavior.STATE_SETTLING;
}
Recall menu:
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Popup menu:
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);