Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation in Android (三)&BottomSheetDialog #125

Open
soapgu opened this issue Mar 23, 2022 · 0 comments
Open

Navigation in Android (三)&BottomSheetDialog #125

soapgu opened this issue Mar 23, 2022 · 0 comments
Labels
mvvm This issue or pull request already exists 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Mar 23, 2022

  • 介绍

对于安卓的模态框,我们用的一直是普通款DialogFragment

对于那种从底部弹出的模态框是怎么实现的那?
官网就特别的不走心

Certain subclasses of DialogFragment, such as BottomSheetDialogFragment, embed your view in a dialog that is styled as a bottom sheet.

就提了一句就完了,点进去也只是类的api的说明,实在不算是啥特别有价值的信息

  • BottomSheetDialogFragment

还是material的官网相对提供的信息相对详尽
有两种方式实现底部滑动表单

  1. 现有View上增加app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    这种做法比较“老式”,而且缺乏独立性。所以不推荐

  2. 使用BottomSheetDialogFragment
    这种实现方式相对比较“符合胃口”
    由于

图片

继承了DialogFragment,所以用法和DialogFragment一摸一样,这里暂时不展开了
  • 与Navigation结合使用

<navigation>
    <dialog
        android:id="@+id/electronicCardFragment"
        android:name="com.space365.mobile.fragments.ElectronicCardFragment"
        android:label="fragment_electronic_card"
        tools:layout="@layout/fragment_electronic_card" />
</navigation>

创建DialogFragment类型的目的地。由于BottomSheetDialogFragment是DialogFragment的派生类,所以也可以加

图片

接下来的设置Action的路径和跳转和前面的逻辑一摸一样。 Navagation组件为“帮“我调用show。堆栈压栈操作等也完全都代劳处理很省心。
  • MVVM支持

由于基类不能用泛型,所以只能另外起一个类了

public class MVVMBottomSheetDialogFragment<VM extends ViewModel> extends BottomSheetDialogFragment {
    private final Class<VM> classOfVM;
    @LayoutRes
    private final int layoutId;
    private final int variableId;
    protected VM viewModel;

    /**
     * 构造函数
     *
     * @param classOfVM  ViewModel的Class
     * @param layoutId   对应layout的id
     * @param variableId layout的变量值 Sample:BR.dataContext
     */
    public MVVMBottomSheetDialogFragment(Class<VM> classOfVM, @LayoutRes int layoutId, int variableId) {
        this.classOfVM = classOfVM;
        this.layoutId = layoutId;
        this.variableId = variableId;
    }

    /**
     * 提供ViewModelStoreOwner给ViewModelProvider
     * extend class call override
     *
     * @return ViewModelStoreOwner
     */
    @NonNull
    protected ViewModelStoreOwner provideViewModelStoreOwner() {
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.viewModel = new ViewModelProvider(this.provideViewModelStoreOwner()).get(classOfVM);
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, container, false);
        binding.setVariable(this.variableId, this.viewModel);
        return binding.getRoot();
    }

}

和DialogFragment相关的MVVM基类高度雷同,暂时找不到更好的抽象方法

  • 解决默认展开尺寸的问题

展开高度是通过peekHeight设置的

默认的设置是auto,设置为16:9对宽高比。
但是我要展开到全屏要怎么弄,只能手工设置了。

这里我们使用的代码方式来实现

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((BottomSheetDialog)requireDialog()).getBehavior().setPeekHeight(1950);
    }

暂时处理得比较粗糙,没考虑不同手机分辨率的情况

@soapgu soapgu added mvvm This issue or pull request already exists 安卓 安卓 labels Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mvvm This issue or pull request already exists 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant