如何禁用BottomSheetDialogFragment
手指拖动?
我看到了类似的问题,但它们都是BottomSheet
而不是BottomSheetDialogFragment
。
答案 0 :(得分:11)
创建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>
您将获得以下输出:
解决方案的一部分来自this回答。
答案 1 :(得分:5)
发布材料设计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)
}
答案 2 :(得分:2)
我的版本。它运作完美。
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.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) {}
})
}
// Do something with your dialog like setContentView() or whatever
return dialog
}
...
}
答案 3 :(得分:2)
只需添加 void PrintEntity(class Entity* e);
您可以在BottomshitDialogFragment中获取BottomshitBehaviour的对象。
bottomSheetBehavior.setHideable(false);
答案 4 :(得分:1)
如果要停用BottomSheetDialog
拖动,请尝试设置setCancelable(false)
。
答案 5 :(得分:1)
评分最高的答案包含样板代码,例如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
答案 6 :(得分:1)
这是azizbekian's答案的Kotlin版本,因为有人问有关使用数据绑定的问题
mydictofdict[1].update({'mykey1':'myval1'})
答案 7 :(得分:0)
这是我设法解决的方法:
df['i']
答案 8 :(得分:0)
我得到了答案here,我刚刚添加了pacakge.json
,使“底页对话框”的片段高度为match_parent,并在显示时变软。
content.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT));
答案 9 :(得分:0)
简单的解决方案
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) {}
});
答案 10 :(得分:0)
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
}
答案 11 :(得分:0)
这是我的解决方案:
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
}
}
}
答案 12 :(得分:0)
先生,这对我有用
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 }
答案 13 :(得分:0)
我的简单解决方案:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle)
dialog.behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
isDraggable = false
}
return dialog
}
答案 14 :(得分:0)
在您的oncreateView
//Kotlin
val baseDialog = dialog
if (baseDialog is BottomSheetDialog) {
baseDialog.behavior.isDraggable = false
}
//If cancelable also not required.
isCancelable = false
答案 15 :(得分:0)
他们也可以通过设置可拖动来做到这一点,所以这是我的另一个例子。
我的BottomSheetDialogFragment里面包含了一个RecyclerView,如果你需要一个BottomSheetDialogFragment禁止向上拖动,可以向下拖动,我做的是把RecyclerView设置为绝对高度
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="367dp"/>
答案 16 :(得分:-1)
为时已晚,但值得分享。
behavior.setDraggable(false)
这行完成了工作。