android - 谷歌资料库更新后, Bottom Sheet View 无法完全展开吗?

标签 android androidx bottom-sheet

早些时候,我使用了 alpha 03 库,该库能够在启动应用程序时扩展 Bottom Sheet View 。

implementation 'com.google.android.material:material:1.1.0-alpha03'



在更新 alpha 04 之后。我发现了我之前使用的代码
无法正常工作,只能看到部分 View 。

implementation 'com.google.android.material:material:1.1.0-alpha04'



但是在向上和向下滑动后, View 可以完美扩展。

首次启动时如何确保 Bottom Sheet 完全可见。先前的代码不正确吗?

这是MainActivity中的代码
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetSummaryLayout);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehavior.setHideable(false);

Bottom Sheet View 的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet_summary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="bottom"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    tool:behavior_peekHeight="300dp">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager_summary"
        android:layout_width="match_parent"
        android:layout_height="321dp"
        android:layout_gravity="bottom"
        android:background="@drawable/bg_transparent_gradient"
        android:clipToPadding="false"
        android:overScrollMode="never"
        android:paddingStart="@dimen/summary_card_left_padding"
        android:paddingLeft="@dimen/summary_card_left_padding"
        android:paddingEnd="@dimen/summary_card_right_padding"
        android:paddingRight="@dimen/summary_card_right_padding" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_summary_bar"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimaryDark"
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp" />

</LinearLayout>

最佳答案

尝试下面的代码,我已经在我的一个项目中使用过,您可能会获得帮助

  public class PopUpFragment extends BottomSheetDialogFragment {

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(final DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;

                bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View view, int i) {

                        switch (i){

                            case BottomSheetBehavior.STATE_HIDDEN:
                                dialog.dismiss();

                                break;
                            default:
                                break;
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View view, float v) {
                        setScrim(v);

                    }
                });
            }
        });

        // Do something with your dialog like setContentView() or whatever
        return dialog;
    }
    private void setScrim(float slideOffset) {
       // bottomSheet.getForeground().setAlpha((int) (slideOffset * 150));


    }

 public static PopUpFragment newInstance() {

        PopUpFragment fragment = new PopUpFragment();

        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.your_layou, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

}

显示BottomSheet
  PopUpFragment bottomDialogFragment =
                PopUpFragment.newInstance();

        bottomDialogFragment.show(getChildFragmentManager(),"PopUpFragment");

关于android - 谷歌资料库更新后, Bottom Sheet View 无法完全展开吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55175696/

相关文章:

android - 淡入淡出 Java 中的 Android 动画

android - 无法解析 androidx.databinding

android - java.lang.IllegalArgumentException : The view is not associated with BottomSheetBehavior 异常

android - setReverseLayout 中的 RecyclerView 滚动到顶部(或底部)

android - 我如何在 android 上录制、加密(在内存中)和多路复用音频和视频而不会使文件不同步?

android - 无法生成 View 绑定(bind) java.lang.StackOverflowError

android - 在 Bottom Sheet 中向下滚动

android - 我怎样才能将 RecyclerView 转发到它的父级 BottomSheet 以便折叠它?

android - Google Cardboard Unity SDK Tilted 无法在设备上运行

Android Studio 无法识别 xml 中的 androidx 首选项库