如何禁用 BottomSheetDialogFragment 拖动
How to disable BottomSheetDialogFragment dragging
如何禁用 BottomSheetDialogFragment
手指拖动?
我看到了类似的问题,但都是关于 BottomSheet
而不是 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>
您将获得以下输出:
部分解法借鉴答案
如果要禁用BottomSheetDialog
拖动,请尝试设置setCancelable(false)
。
这是 答案的 Kotlin 版本,因为有人询问过使用数据绑定
@SuppressLint("RestrictedApi")
override fun setupDialog(d: Dialog?, style: Int) {
super.setupDialog(d, style)
dialogExampleBinding = DataBindingUtil
.inflate(LayoutInflater.from(context), R.layout.dialogExample, null, false) //This is for data binding only
d?.setContentView(R.layout.dialogExample)
val myDialog:BottomSheetDialog = d as BottomSheetDialog
val dField = myDialog.javaClass.getDeclaredField("behavior") //This is the correct name of the variable in the BottomSheetDialog class
dField.isAccessible = true
val behavior = dField.get(d) as BottomSheetBehavior<*>
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
我的版本。它工作得很好。
编辑 09/04/2020: 将折旧 setBottomSheetCallback()
替换为 addBottomSheetCallback()
class FragMenuBDrawer : BottomSheetDialogFragment() {
...
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener {
val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
...
}
只需添加 bottomSheetBehavior.setHideable(false);
您可以在 BottomSheetDialogFragment
中获取 BottomSheetBehaviour
的对象。
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
View parent = (View) view.getParent();
getHeight(view);
((BottomSheetBehavior) behavior).setFitToContents(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
bottomSheetBehavior.setHideable(false);
我得到答案 here,我刚刚添加 content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
使底部 Sheet 对话框片段高度为 match_parent 并在显示时使其变软。
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
// view hierarchy is inflated after dialog is shown
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
//this disables outside touch
d.getWindow().findViewById(R.id.touch_outside).setOnClickListener(null);
//this prevents dragging behavior
View content = d.getWindow().findViewById(R.id.design_bottom_sheet);
content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
((CoordinatorLayout.LayoutParams) content.getLayoutParams()).setBehavior(null);
}
});
return d;
}
为时已晚但值得分享。
behavior.setDraggable(false)
这一行完成了工作。
这是我设法修复它的方法:
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
mBehavior.setHideable(false);
评分最高的答案包含样板代码,例如 Field 和 try-catches。
所以这里有一个更好的 Kotlin 版本:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState).apply {
setOnShowListener(::onShow)
}
}
private fun onShow(dialogInterface: DialogInterface) {
val dialog = dialogInterface as BottomSheetDialog
val frameLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
?: return
BottomSheetBehavior.from(frameLayout).apply {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING)
state = BottomSheetBehavior.STATE_EXPANDED
}
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
})
}
}
在BottomSheetDialogFragment
中使用
在 material 设计 1.2.0 发布后,有更简单的方法可以实现相同的目的。
从 BottomSheetDialogFragment
呼叫时:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheetDialog.setOnShowListener {
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
if (bottomSheet != null) {
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
behavior.isDraggable = false
}
}
return bottomSheetDialog
}
或使用样式:
<style name="SomeStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="behavior_draggable">false</item>
</style>
然后在对话片段的 onCreate
中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.SomeStyle)
}
简单的解决方案
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) {}
});
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Disable dragging by set isDraggable to false
val bottomSheetDialog = dialog as BottomSheetDialog
val bottomSheetBehavior = bottomSheetDialog.behavior
bottomSheetBehavior.isDraggable = false
}
这是我的解决方案:
setOnShowListener {
Handler().post {
val bottomSheet = findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
bottomSheet?.let {
BottomSheetBehavior.from(it).state = STATE_EXPANDED
// Disable dialog dragging behavior which causes issue on EditText scroll!
BottomSheetBehavior.from(it).isDraggable = false
}
}
}
这对我有用,先生
override fun onCreateDialog(savedInstanceState : Bundle?) : Dialog {
val dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener {
val bottomSheetDialog : BottomSheetDialog = it as BottomSheetDialog;
var bottomSheetBehavior = BottomSheetBehavior<FrameLayout>();
bottomSheetBehavior = bottomSheetDialog.getBehavior()
bottomSheetBehavior.setDraggable(false);
}
return dialog }
我的简单解决方案:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle)
dialog.behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
isDraggable = false
}
return dialog
}
在你的oncreateView
//Kotlin
val baseDialog = dialog
if (baseDialog is BottomSheetDialog) {
baseDialog.behavior.isDraggable = false
}
//If cancelable also not required.
isCancelable = false
他们也可以通过设置可拖动来做到这一点,所以这是我的另一个例子。
我的BottomSheetDialogFragment里面包含一个RecyclerView,如果你需要一个BottomSheetDialogFragment禁止向上拖,可以向下拖,我做的是我把RecyclerView设置为一个绝对高度
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="367dp"/>
如果您只想禁用 bottomsheet 的关闭,只需将 isCancelable false 设置为片段实例即可
val bSheet = BottomSheetFragment.newInstance("") bSheet.isCancelable = false bSheet.show(supportFragmentManager, "sheet")
如何禁用 BottomSheetDialogFragment
手指拖动?
我看到了类似的问题,但都是关于 BottomSheet
而不是 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>
您将获得以下输出:
部分解法借鉴
如果要禁用BottomSheetDialog
拖动,请尝试设置setCancelable(false)
。
这是
@SuppressLint("RestrictedApi")
override fun setupDialog(d: Dialog?, style: Int) {
super.setupDialog(d, style)
dialogExampleBinding = DataBindingUtil
.inflate(LayoutInflater.from(context), R.layout.dialogExample, null, false) //This is for data binding only
d?.setContentView(R.layout.dialogExample)
val myDialog:BottomSheetDialog = d as BottomSheetDialog
val dField = myDialog.javaClass.getDeclaredField("behavior") //This is the correct name of the variable in the BottomSheetDialog class
dField.isAccessible = true
val behavior = dField.get(d) as BottomSheetBehavior<*>
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
我的版本。它工作得很好。
编辑 09/04/2020: 将折旧 setBottomSheetCallback()
替换为 addBottomSheetCallback()
class FragMenuBDrawer : BottomSheetDialogFragment() {
...
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener {
val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
...
}
只需添加 bottomSheetBehavior.setHideable(false);
您可以在 BottomSheetDialogFragment
中获取 BottomSheetBehaviour
的对象。
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
View parent = (View) view.getParent();
getHeight(view);
((BottomSheetBehavior) behavior).setFitToContents(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
bottomSheetBehavior.setHideable(false);
我得到答案 here,我刚刚添加 content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
使底部 Sheet 对话框片段高度为 match_parent 并在显示时使其变软。
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
// view hierarchy is inflated after dialog is shown
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
//this disables outside touch
d.getWindow().findViewById(R.id.touch_outside).setOnClickListener(null);
//this prevents dragging behavior
View content = d.getWindow().findViewById(R.id.design_bottom_sheet);
content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
((CoordinatorLayout.LayoutParams) content.getLayoutParams()).setBehavior(null);
}
});
return d;
}
为时已晚但值得分享。
behavior.setDraggable(false)
这一行完成了工作。
这是我设法修复它的方法:
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
mBehavior.setHideable(false);
评分最高的答案包含样板代码,例如 Field 和 try-catches。
所以这里有一个更好的 Kotlin 版本:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState).apply {
setOnShowListener(::onShow)
}
}
private fun onShow(dialogInterface: DialogInterface) {
val dialog = dialogInterface as BottomSheetDialog
val frameLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
?: return
BottomSheetBehavior.from(frameLayout).apply {
addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_DRAGGING)
state = BottomSheetBehavior.STATE_EXPANDED
}
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
})
}
}
在BottomSheetDialogFragment
在 material 设计 1.2.0 发布后,有更简单的方法可以实现相同的目的。
从 BottomSheetDialogFragment
呼叫时:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheetDialog.setOnShowListener {
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
if (bottomSheet != null) {
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
behavior.isDraggable = false
}
}
return bottomSheetDialog
}
或使用样式:
<style name="SomeStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="behavior_draggable">false</item>
</style>
然后在对话片段的 onCreate
中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.SomeStyle)
}
简单的解决方案
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) {}
});
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Disable dragging by set isDraggable to false
val bottomSheetDialog = dialog as BottomSheetDialog
val bottomSheetBehavior = bottomSheetDialog.behavior
bottomSheetBehavior.isDraggable = false
}
这是我的解决方案:
setOnShowListener {
Handler().post {
val bottomSheet = findViewById<View>(R.id.design_bottom_sheet) as? FrameLayout
bottomSheet?.let {
BottomSheetBehavior.from(it).state = STATE_EXPANDED
// Disable dialog dragging behavior which causes issue on EditText scroll!
BottomSheetBehavior.from(it).isDraggable = false
}
}
}
这对我有用,先生
override fun onCreateDialog(savedInstanceState : Bundle?) : Dialog {
val dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener {
val bottomSheetDialog : BottomSheetDialog = it as BottomSheetDialog;
var bottomSheetBehavior = BottomSheetBehavior<FrameLayout>();
bottomSheetBehavior = bottomSheetDialog.getBehavior()
bottomSheetBehavior.setDraggable(false);
}
return dialog }
我的简单解决方案:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle)
dialog.behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
isDraggable = false
}
return dialog
}
在你的oncreateView
//Kotlin
val baseDialog = dialog
if (baseDialog is BottomSheetDialog) {
baseDialog.behavior.isDraggable = false
}
//If cancelable also not required.
isCancelable = false
他们也可以通过设置可拖动来做到这一点,所以这是我的另一个例子。
我的BottomSheetDialogFragment里面包含一个RecyclerView,如果你需要一个BottomSheetDialogFragment禁止向上拖,可以向下拖,我做的是我把RecyclerView设置为一个绝对高度
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="367dp"/>
如果您只想禁用 bottomsheet 的关闭,只需将 isCancelable false 设置为片段实例即可
val bSheet = BottomSheetFragment.newInstance("") bSheet.isCancelable = false bSheet.show(supportFragmentManager, "sheet")