如何禁止使用bottomsheetdialogfragment拖动?
生活随笔
收集整理的這篇文章主要介紹了
如何禁止使用bottomsheetdialogfragment拖动?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
myactivity代碼如下:
public class MyActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my);new MyBottomSheetFragment().show(getSupportFragmentManager(), "tag");}public static class MyBottomSheetFragment extends BottomSheetDialogFragment {@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);bottomSheetDialog.setContentView(R.layout.sample);try {Field mBehaviorField = bottomSheetDialog.getClass().getDeclaredField("mBehavior");mBehaviorField.setAccessible(true);final BottomSheetBehavior behavior = (BottomSheetBehavior) mBehaviorField.get(bottomSheetDialog);behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {@Overridepublic void onStateChanged(@NonNull View bottomSheet, int newState) {if (newState == BottomSheetBehavior.STATE_DRAGGING) {behavior.setState(BottomSheetBehavior.STATE_EXPANDED);}}@Overridepublic void onSlide(@NonNull View bottomSheet, float slideOffset) {}});} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}return bottomSheetDialog;}}} 復制代碼這個地方的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"><Viewandroid:layout_width="match_parent"android:layout_height="100dp"android:background="#e479da" /><Viewandroid:layout_width="match_parent"android:layout_height="100dp"android:background="#798de4" /><Viewandroid:layout_width="match_parent"android:layout_height="100dp"android:background="#e4db79" /></LinearLayout>復制代碼你會得到下面的輸出:
圖片描述 原文地址:http://www.apkbus.com/blog-919651-76640.html總結
以上是生活随笔為你收集整理的如何禁止使用bottomsheetdialogfragment拖动?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鉴别正宗官换机
- 下一篇: Android中文API(99)—— R