拖动向下关闭BottomSheetDialog后仍然保持隐藏状态

18

我对BottomSheetDialog被关闭时的行为非常好奇:当用户向下拖动以隐藏它时,它将保持隐藏状态,即使之后调用bottomSheetDialog#show()也不会显示出来。这种情况只会在向下拖动时发生,而不是用户点击外部区域或程序调用bottomSheetDialog#dismiss()时。

这真的很烦人,因为我有一个里面有RecyclerView的相当大的bottomSheetDialog,每次想要显示bottomSheetDialog时都必须创建一个新的。

因此,与其只是这样做:

if(bottomSheetDialog != null){
   bottomSheetDialog.show();
else{
   createNewBottomSheetDialog();
}

每次我都必须创建一个新的。

我是漏掉了些什么,还是这是正常行为?(顺便说一句,我使用 appcompat-v7:23.2.1 )。

3个回答

13

我最终通过直接查看BottomSheetDialog的实现来解决了这个问题,发现它只是将一个普通的Dialog包装在一个常规的BottomSheet中。
问题出在BottomSheetCallBack中:

@Override
    public void onStateChanged(@NonNull View bottomSheet,
            @BottomSheetBehavior.State int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
    }

当对话框被拖动下来以解除时,状态达到了HIDDEN,这时会出现问题。在此之后,即使调用bottomSheetDialog.show(),对话框仍然保持隐藏状态。我找到的最简单的解决方法是删除该状态并将其替换为COLLAPSED状态。

我创建了一个类,复制了整个类,并添加了一行代码来解决该问题:

@Override
    public void onStateChanged(@NonNull View bottomSheet,
                               @BottomSheetBehavior.State int newState) {

        if (newState == CustomBottomSheetBehavior.STATE_HIDDEN) {

            dismiss();
            bottomSheetBehavior.setState(CustomBottomSheetBehavior.STATE_COLLAPSED);
        }
    }

这里是最终代码:

public class CustomBottomSheetDialog extends AppCompatDialog {

    public CustomBottomSheetDialog (@NonNull Context context) {
        this(context, 0);
    }

    public CustomBottomSheetDialog (@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 CustomBottomSheetDialog (@NonNull Context context, boolean cancelable,
            OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResId) {
        super.setContentView(wrapInBottomSheet(layoutResId, null, null));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().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));
    }

    private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
        final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
                R.layout.design_bottom_sheet_dialog, null);
        if (layoutResId != 0 && view == null) {
            view = getLayoutInflater().inflate(layoutResId, coordinator, false);
        }
        FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
        if (params == null) {
            bottomSheet.addView(view);
        } else {
            bottomSheet.addView(view, params);
        }
        // We treat the CoordinatorLayout as outside the dialog though it is technically inside
        if (shouldWindowCloseOnTouchOutside()) {
            coordinator.findViewById(R.id.touch_outside).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (isShowing()) {
                                cancel();
                            }
                        }
                    });
        }
        return coordinator;
    }

    private boolean shouldWindowCloseOnTouchOutside() {
        if (Build.VERSION.SDK_INT < 11) {
            return true;
        }
        TypedValue value = new TypedValue();
        //noinspection SimplifiableIfStatement
        if (getContext().getTheme()
                .resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)) {
            return value.data != 0;
        }
        return false;
    }

    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) {
                dismiss();
                bottomSheetBehavior.setState(CustomBottomSheetBehavior.STATE_COLLAPSED);
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };

}

我无法复制和粘贴此代码,因为在设置状态时没有引用bottomSheetBehavior。 - Zeek Aran

8

更新:该问题在某个版本的支持库中得到解决。我并不知道确切的修复版本,但在27.0.2中已经修复了。

注意:由于 Google 对 URL 架构进行了一些修改,因此该 URL 不再指向所描述的问题。

有一个比复制整个类只添加一行更好的解决方法。

// Fix BottomSheetDialog not showing after getting hidden when the user drags it down
    myBottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
            FrameLayout bottomSheet = (FrameLayout) bottomSheetDialog
                    .findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    });

请查看:https://code.google.com/p/android/issues/detail?id=202396#c7

在onShow中将状态设置为expanded并不能解决任何问题,你必须进入bottomSheetcallback来移除每当它被设置时的隐藏状态。 - David Seroussi
1
@DavidSeroussi 我已经亲自测试过了,它运行良好。 - Kh5

0

我有一个示例演示,希望它有用。

public class BottomListMenu extends BottomSheetDialog {
    private List<MenuDTO> menuList;
    private OnMenuItemTapped menuTapListener;

    public BottomListMenu(@NonNull Context context, List<MenuDTO> menuList, OnMenuItemTapped menuTapListener) {
        super(context);
        this.menuList = menuList;
        this.menuTapListener = menuTapListener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_menu_list);
        RecyclerView rcvList = (RecyclerView) findViewById(R.id.rcv_menu_list);
        rcvList.setLayoutManager(new LinearLayoutManager(getContext()));
        BottomSheetMenuListAdapter adapter = new BottomSheetMenuListAdapter(getContext(), this, menuList, menuTapListener);
        rcvList.setAdapter(adapter);
    }
}

--- 使用 ---

BottomListMenu menu = new BottomListMenu(MainActivity.this, MenuUtils.getListMenu(MainActivity.this), new OnMenuItemTapped() {
    @Override
    public void onClickMenuItem(MenuDTO menu) {
        if (menu.getMenuTitle().equals(getString(R.string.menu_edit))) {
            Toast.makeText(MainActivity.this, "Edit Clicked", Toast.LENGTH_SHORT).show();
        } else if (menu.getMenuTitle().equals(getString(R.string.menu_delete))) {
            Toast.makeText(MainActivity.this, "Delete Clicked", Toast.LENGTH_SHORT).show();
        } else if (menu.getMenuTitle().equals(getString(R.string.menu_attach))) {
            Toast.makeText(MainActivity.this, "Attach Clicked", Toast.LENGTH_SHORT).show();
        }
    }
});
menu.show();

-- 完整样例代码在此处提供 --

https://github.com/bita147/BottomSheetDialog


谢谢你,但这怎么回答我的问题呢? - David Seroussi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接