BottomSheetDialogFragment使用

tech2025-09-03  53

BottomSheetDialogFragment继承于DialogFragment,一个Dialog形式的framgnet,可实现拖动打开及关闭 如源码所示,在BottonSheetDialogFragment内部是创建了一个BottomSheetDialog

public class BottomSheetDialogFragment extends AppCompatDialogFragment { public BottomSheetDialogFragment() { } public Dialog onCreateDialog(Bundle savedInstanceState) { return new BottomSheetDialog(this.getContext(), this.getTheme()); } }

1.使用CoordinatorLayout作为根布局,给下属的layout添加属性app:layout_behavior="@string/bottom_sheet_behavior",此layout只允许有一个child,否则会报错 2.setPeekHeight设置弹窗的默认高度,直接使用是不起作用的,需要自定义BottomSheetDialog

private BottomSheetBehavior<FrameLayout> mBehavior; boolean mCancelable = true; private boolean mCanceledOnTouchOutside = true; private boolean mCanceledOnTouchOutsideSet; public MyBottomSheetDialog(@NonNull Context context) { this(context, 0); } public MyBottomSheetDialog(@NonNull Context context, @StyleRes int theme) { super(context, getThemeResId(context, theme)); // We hide the title bar for any style configuration. Otherwise, there will be a gap // above the bottom sheet when it is expanded. supportRequestWindowFeature(Window.FEATURE_NO_TITLE); } protected MyBottomSheetDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); mCancelable = cancelable; } @Override public void setContentView(@LayoutRes int layoutResId) { super.setContentView(wrapInBottomSheet(layoutResId, null, null)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); if (window != null) { if (Build.VERSION.SDK_INT >= 21) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); } window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); } } @Override public void setContentView(View view) { super.setContentView(wrapInBottomSheet(0, view, null)); } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { super.setContentView(wrapInBottomSheet(0, view, params)); } @Override public void setCancelable(boolean cancelable) { super.setCancelable(cancelable); if (mCancelable != cancelable) { mCancelable = cancelable; if (mBehavior != null) { mBehavior.setHideable(cancelable); } } } @Override protected void onStart() { super.onStart(); if (mBehavior != null) { mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); } } @Override public void setCanceledOnTouchOutside(boolean cancel) { super.setCanceledOnTouchOutside(cancel); if (cancel && !mCancelable) { mCancelable = true; } mCanceledOnTouchOutside = cancel; mCanceledOnTouchOutsideSet = true; } @SuppressLint("WrongConstant") private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) { final FrameLayout container = (FrameLayout) View.inflate(getContext(), R.layout.dialog_bottom_sheet, null); final CoordinatorLayout coordinator = (CoordinatorLayout) container.findViewById(R.id.coordinator); if (layoutResId != 0 && view == null) { view = getLayoutInflater().inflate(layoutResId, coordinator, false); } FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet); CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams(); layoutParams.height = (int)(getHeight()*0.92); //设置弹窗的最大高度 // layoutParams.gravity = Gravity.TOP|Gravity.CENTER_HORIZONTAL; bottomSheet.setLayoutParams(layoutParams); mBehavior = BottomSheetBehavior.from(bottomSheet); mBehavior.setBottomSheetCallback(mBottomSheetCallback); mBehavior.setHideable(mCancelable); mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); mBehavior.setPeekHeight((int)(getHeight()*0.6)); if (params == null) { bottomSheet.addView(view); } else { bottomSheet.addView(view, params); } // We treat the CoordinatorLayout as outside the dialog though it is technically inside coordinator.findViewById(R.id.touch_outside).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mCancelable && isShowing() && shouldWindowCloseOnTouchOutside()) { cancel(); } } }); // Handle accessibility events ViewCompat.setAccessibilityDelegate(bottomSheet, new AccessibilityDelegateCompat() { @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); if (mCancelable) { info.addAction(AccessibilityNodeInfoCompat.ACTION_DISMISS); info.setDismissable(true); } else { info.setDismissable(false); } } @Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (action == AccessibilityNodeInfoCompat.ACTION_DISMISS && mCancelable) { cancel(); return true; } return super.performAccessibilityAction(host, action, args); } }); bottomSheet.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { // Consume the event and prevent it from falling through return true; } }); return container; } boolean shouldWindowCloseOnTouchOutside() { if (!mCanceledOnTouchOutsideSet) { if (Build.VERSION.SDK_INT < 11) { mCanceledOnTouchOutside = true; } else { TypedArray a = getContext().obtainStyledAttributes( new int[]{android.R.attr.windowCloseOnTouchOutside}); mCanceledOnTouchOutside = a.getBoolean(0, true); a.recycle(); } mCanceledOnTouchOutsideSet = true; } return mCanceledOnTouchOutside; } private static int getThemeResId(Context context, int themeId) { if (themeId == 0) { // If the provided theme is 0, then retrieve the dialogTheme from our theme TypedValue outValue = new TypedValue(); if (context.getTheme().resolveAttribute( R.attr.bottomSheetDialogTheme, outValue, true)) { themeId = outValue.resourceId; } else { // bottomSheetDialogTheme is not provided; we default to our light theme themeId = R.style.Theme_Design_Light_BottomSheetDialog; } } return themeId; } private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) { if (newState == BottomSheetBehavior.STATE_HIDDEN) { cancel(); } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { } }; private int getHeight() { return getContext().getResources().getDisplayMetrics().heightPixels; }

3.在BottomSheetDialogFragment中使用ViewPager+RecyclerView,只有第一个页面能滚动,其他页面不可以。原因是在BottomSheetBehavior中无论多少个页面,只返回第一个可滚动的页面,如下源码所示

@VisibleForTesting View findScrollingChild(View view) { if (ViewCompat.isNestedScrollingEnabled(view)) { return view; } else { if (view instanceof ViewGroup) { ViewGroup group = (ViewGroup)view; int i = 0; for(int count = group.getChildCount(); i < count; ++i) { View scrollingChild = this.findScrollingChild(group.getChildAt(i)); if (scrollingChild != null) { return scrollingChild; } } } return null; } }

解决办法: (1)重写BottomSheetBehavior的此方法,在viewpager选择页面的时候调用此方法,发现并没有用

@VisibleForTesting View findScrollingChild(View view) { if (ViewCompat.isNestedScrollingEnabled(view)) { return view; } if (view instanceof ViewPager) { ViewPager viewPager = (ViewPager) view; View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem()); // View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager); if (currentViewPagerChild == null) { return null; } View scrollingChild = findScrollingChild(currentViewPagerChild); if (scrollingChild != null) { return scrollingChild; } } else if (view instanceof ViewGroup) { ViewGroup group = (ViewGroup) view; for (int i = 0, count = group.getChildCount(); i < count; i++) { View scrollingChild = findScrollingChild(group.getChildAt(i)); if (scrollingChild != null) { return scrollingChild; } } } return null; }

(2)在ViewPager的adapter类中,重写setPrimaryItem(),在此方法中,设置切换页面后其他页面不可滑动,只允许当前页面滑动;

@Override public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) { super.setPrimaryItem(container, position, object); Fragment currentFragment = (Fragment) object; if (currentFragment.getView() != null) { try { for (int i = 0; i < getCount(); i++) { ((RecyclerView) ((PullToRefreshLayout) ((RelativeLayout) container.getChildAt(i)).getChildAt(0)).getChildAt(1)).setNestedScrollingEnabled(false); } RelativeLayout currentNestedScrollView = ((RelativeLayout) currentFragment.getView()); ((RecyclerView) ((PullToRefreshLayout) currentNestedScrollView.getChildAt(0)).getChildAt(1)).setNestedScrollingEnabled(true); container.requestLayout(); } catch (Exception e) { } } }
最新回复(0)