由于我声誉低,我无法发表评论。所以我要扩展这个问题: Disabling User dragging on BottomSheet
Ray W提供的解决方案有效但现在通过在父视图(CoordinatorLayout)上滑动和拖动来扩展。
在该图像中,如果我拖动"不需要的拖动区域",则BottomSheet向上滑动。如何在不受欢迎的视图中过滤掉或停止触摸事件?
答案 0 :(得分:2)
对于Kotlin,只需在代码中添加以下行,
behaviour.isDraggable = false
答案 1 :(得分:0)
将onInterceptTouchEvent函数返回值从Ray W solution更改为:
for ( var index = 0; index < 5; index++ ) {
var myVar = document.createElement('p');
myVar.id = 'myVarID' + index;
myVar.name = 'myVarName' + index;
//Send request to server
var data = {};
var i = 'ind';
var id = myVar.id;
var name = myVar.name;
data[id] = name;
data[i] = index;
$.post("script.php", data, function(data){
console.log("Server response:", data);
});
}
还有一件事,如果你在BottomSheetLayout中有一个ListView,那么在ListView中滚动项会将bottomSheetBehavior状态改为&#34; STATE_DRAGGING&#34;来自&#34; STATE_EXPANDED&#34;。 示例代码:
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
return super.onInterceptTouchEvent(parent, child, event) && mAllowUserDragging;
}
答案 2 :(得分:0)
简单的解决方案,它解决了我遇到的两个问题。
1-它阻止了拖动事件。
2-解决了listView滚动问题
CoordinatorLayout.Behavior<View> behavior;
View profile_config_layout_bottom_sheet = findViewById(R.id.list_view_audience_profile_config_layout);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) profile_config_layout_bottom_sheet.getLayoutParams();
behavior = layoutParams.getBehavior();
assert behavior != null;
((BottomSheetBehavior<View>) behavior).addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
((BottomSheetBehavior<View>) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
});
显示对话框:
(((BottomSheetBehavior)行为).setState(BottomSheetBehavior.STATE_EXPANDED);
答案 3 :(得分:-3)
看看这个:
SELECT @currank := 0
onBackPress时关闭BottomSheet:
mBottomSheetBehavior1.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
mBottomSheetBehavior1.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
Log.i("BottomSheetCallback", "slideOffset: " + slideOffset);
}
});