问题
I need the bottom sheet to stop at two positions. I have the following code for Bottom Sheet.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
....
</RelativeLayout>
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="1000dp"
android:orientation="vertical">
....
</LinearLayout>
</ScrollView>
</FrameLayout>
and
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
// React to state change
Log.e("onStateChanged", "onStateChanged:" + newState);
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setPeekHeight(600);
showAgain.setVisibility(View.GONE);
mMap.getUiSettings().setScrollGesturesEnabled(false);
} else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
if (behavior.getPeekHeight() == 600) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
behavior.setPeekHeight(80);
mMap.getUiSettings().setScrollGesturesEnabled(false);
} else if (behavior.getPeekHeight() == 80) {
showAgain.setVisibility(View.VISIBLE);
mMap.getUiSettings().setScrollGesturesEnabled(true);
}
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// React to dragging events
Log.e("onSlide", "onSlide " + slideOffset);
}
});
behavior.setPeekHeight(600);
This code works fine except one thing. The very first time I have to scroll up the bottom sheet and then I can down scroll it. I cannot directly down scroll the sheet.
Any help will be appreciated.
回答1:
Instead of using scrollview you can use NestedScrollView that works better with CoordinatorLayout make sure to use app:layout_behavior="@string/appbar_scrolling_view_behavior" for smooth scrolling of content inside NestedScrollView
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
mBottomSheetBehavior.setPeekHeight(0);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_negetive: {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
mBottomSheetBehavior.setPeekHeight(Constants.PEEK_HEIGHT);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
}
case R.id.btn_positive: {
//some code
}
}
}
the above solution worked for me using this link: https://code.tutsplus.com/articles/how-to-use-bottom-sheets-with-the-design-support-library--cms-26031
回答2:
Try setting:
app:behavior_hideable="true"
in your xml file and in your activity add something like:
else if (newState == BottomSheetBehavior.STATE_HIDDEN{
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetBehavior.setPeekHeight(80); }
This will allow your bottom sheet dialog to be pulled down immediately if you wanted to.
回答3:
Try this, Hope it will help
Put this after initilizing the views
ViewCompat.postOnAnimation(coordinatorLayout, new Runnable() {
@Override
public void run() {
ViewCompat.postInvalidateOnAnimation(coordinatorLayout);
}
});
来源:https://stackoverflow.com/questions/37741627/bottom-sheet-android-scrolling-issue