本文介绍了如何禁用BottomSheetDialogFragment拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何禁用BottomSheetDialogFragment
用手指拖动?
我看到了类似的问题,但它们都是关于BottomSheet
而不是BottomSheetDialogFragment
的问题.
I saw similar questions, but they're all about BottomSheet
not BottomSheetDialogFragment
.
推荐答案
已创建MyActivity
如下:
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
new MyBottomSheetFragment().show(getSupportFragmentManager(), "tag");
}
public static class MyBottomSheetFragment extends BottomSheetDialogFragment {
@Override
public void setupDialog(Dialog dialog, int style) {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialog;
bottomSheetDialog.setContentView(R.layout.sample);
try {
Field behaviorField = bottomSheetDialog.getClass().getDeclaredField("behavior");
behaviorField.setAccessible(true);
final BottomSheetBehavior behavior = (BottomSheetBehavior) behaviorField.get(bottomSheetDialog);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING{
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
其中R.layout.sample
是简单的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#e479da" />
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#798de4" />
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#e4db79" />
</LinearLayout>
您将获得以下输出:
这篇关于如何禁用BottomSheetDialogFragment拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!