我制作了一个包含两个片段的 Activity。
在 Activity 类中,我为 commonBottomSheet 编写了这段代码:-
BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_pannel_layout));
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetCallback);
在 Co-ordinator 布局中的 Activity xml 文件中,我已将以下布局包括在内:-
<include layout="@layout/bottom_sheet_pannel"/>
在 CommonBottomSheetFragment 中,您可以创建布局。
我的bottomSheet的xml文件(bottom_sheet_pannel)是这样的:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_pannel_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="45dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:id="@+id/grabber_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_vector_slider_grabber"
android:tint="@color/colorTint" />
<fragment
android:id="@+id/rf_common_details_fragment"
android:layout_marginTop="@dimen/margin_10"
android:name="com.fragment.CommonBottomSheetFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
你可以用下面的callBack改变bottomSheet的状态:-
private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View bottomSheet, int newState) {
// do what you want on state change
}
@Override
public void onSlide(View bottomSheet, float slideOffset) {
}
};