【问题标题】:Android BottomSheetDialogFragment does not expand completelyAndroid BottomSheetDialogFragment 没有完全展开
【发布时间】:2016-05-31 20:52:04
【问题描述】:

我有以下测试底部表实现。

当我将 peekHeight 设置为小于 500 的值时,它可以工作。在某个值之后,窥视高度的任何增加都不会改变底部工作表的扩展方式。它只是保留在那里仅手动拖动。我们如何以编程方式设置 peekHeight 以确保底部工作表自动展开到 peek 高度。

bottom_sheet_dialog_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/locUXCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/locUXView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="1 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="2 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="3 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="4 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="5 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="6 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="7 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="8 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="9 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="First Value" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Java 代码

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    private static BottomSheetBehavior bottomSheetBehavior;
    private static View bottomSheetInternal;
    private static MyBottomSheetDialogFragment INSTANCE;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;
                CoordinatorLayout coordinatorLayout = (CoordinatorLayout)d.findViewById(R.id.locUXCoordinatorLayout);
                bottomSheetInternal = d.findViewById(R.id.locUXView);
                bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
                bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
                bottomSheetInternal.requestLayout();
                coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight();
                Toast.makeText(getActivity(), "Height is" + bottomSheetInternal.getHeight() + "  " + coordinatorLayout.getLayoutParams().height, Toast.LENGTH_LONG).show();

            }
        });
        INSTANCE = this;
        return inflater.inflate(R.layout.bottom_sheet_dialog_main, container, false);
    }
}

【问题讨论】:

  • 您确实意识到窥视高度是“折叠”的工作表的高度并且只是显示其中的一部分?如果您将窥视高度设置为工作表本身的高度(或更大)......那么整个“窥视”是无用的。您能否解释一下您要做什么
  • 谢谢。是的,我意识到 peekHeight 是为折叠视图设置的。无论如何,我能够纠正这个问题
  • 这一行对我来说是个窍门:coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight();"

标签: java android android-support-library android-support-design bottom-sheet


【解决方案1】:

在 onCreateView 中使用此代码。

getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet);
            CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
            bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
            coordinatorLayout.getParent().requestLayout();
        }
    });

【讨论】:

  • 在我的情况下,这些代码应该在onActivityCreated 中调用然后完美运行
  • 不错的解决方案和我一起工作,我把代码放在onViewCreated()
【解决方案2】:

我找到了另一个解决方案。也许对未来的读者来说它会很有用。

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    final View root = View.inflate(getContext(), R.layout.fragment_bottom_sheet_choose_time, null);
    dialog.setContentView(root);
    initView(root);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) root.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = params.getBehavior();

    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        mBottomSheetBehavior = (BottomSheetBehavior) behavior;
        mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);

        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int height = root.getMeasuredHeight();
                mBottomSheetBehavior.setPeekHeight(height);
            }
        });
    }
}

正如@Anthonyeef 提到的,这里的ViewTreeObserver 旨在在真正测量视图后获得精确的测量高度,并删除GlobalOnLayoutListener 以获得更好的性能。

但是,在生产中使用之前,请在不同的设备和屏幕上测试此解决方案,因为如果底部页面中的内容高于屏幕,则会产生一些奇怪的滑动行为。

【讨论】:

  • 这个答案真的解决了我的问题。如果有更多的解释,那就太好了。例如,我自己搜索并测试,最终发现 ViewTreeObserver 旨在在视图真正测量后获得精确的测量高度,并删除 GlobalOnLayoutListener 以获得更好的性能。
  • 我收到了一些崩溃报告,其中向 CoordinatorLayout.LayoutParams 的转换失败。在某些情况下,在某些设备上,view.getParent() 似乎返回一个 LinearLayout 而不是 CoordinatorLayout。目前,崩溃报告仅适用于部分 Android 8.0 设备。
  • setupDailogonCreateDialog有什么区别?
【解决方案3】:

通过更深入的 UI 检查,我们发现还有另一个 CoordinatorLayout 包裹了我们的协调器布局。父级CoordinatorLayout 有一个FrameLayout 和一个BottomSheetBehavior,ID 为design_bottom_sheet。由于 FrameLayout 的 id 为 design_bottom_sheetmatch_parent 高度,我们上面代码中设置的窥视高度受到了限制

通过使用 id design_bottom_sheet 设置FrameLayout 的窥视高度,解决了这个问题

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            coordinatorLayout = (CoordinatorLayout) d.findViewById(R.id.locUXCoordinatorLayout);
            bottomSheetInternal = d.findViewById(R.id.locUXView);
            bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
            bottomSheetBehavior.setHidable(false);
            BottomSheetBehavior.from((View)coordinatorLayout.getParent()).setPeekHeight(bottomSheetInternal.getHeight());
            bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
            coordinatorLayout.getParent().requestLayout();

        }
    });

【讨论】:

  • 此解决方案在 R.id.locUXCoordinatorLayout 中给出错误,无法解析符号。我们必须在这里使用哪个 R..?
  • locUXCoordinatorLayout ,这是我认为的 id。你可能看不到它。请参阅我在问题中附加的代码。如果如果解决了您的问题,如果您支持答案,将不胜感激
【解决方案4】:

Kotlin 实现此目的的安全方法是:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog.setOnShowListener {
        val dialog = it as BottomSheetDialog
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet?.let { sheet ->
            dialog.behavior.peekHeight = sheet.height
            sheet.parent.parent.requestLayout()
        }
    }
}

注意:无需将您的布局包装在协调器布局中。

像魅力一样工作。

【讨论】:

  • 我很难找到这个!谢谢!
【解决方案5】:

感谢@athysirus 提供的简洁方法。这是我最终得到的版本,以防有人想要一个工作的 kotlin 示例。

需要注意的是,一旦完成,您还应该删除全局布局侦听器。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            val bottomSheet = (dialog as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            BottomSheetBehavior.from<View>(bottomSheet).apply {
                state = BottomSheetBehavior.STATE_EXPANDED
                peekHeight = 0
            }
            view.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })

【讨论】:

  • 但是这个解决方案会产生另一个问题 - 在交互式地向下移动 BottomSheetDialog 之后,背景窗口仍然变暗。
  • 是的,它仍然变暗
【解决方案6】:

这就是我在BottomSheetDialogFragment 中将peek_heightlayout_height 设置为底部工作表视图的方式

dialog?.setOnShowListener {
                val dialog = dialog as BottomSheetDialog
                val bottomSheet = dialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)
                val coordinatorLayout = bottomSheet?.parent as? CoordinatorLayout
                val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
                bottomSheet?.viewTreeObserver?.addOnGlobalLayoutListener {
                    bottomSheet.viewTreeObserver.removeOnGlobalLayoutListener {}
                    bottomSheetBehavior.peekHeight = getPopupHeight(.5f)
                    val params = bottomSheet.layoutParams
                    params.height = getPopupHeight(1f)
                    bottomSheet.layoutParams = params
                    coordinatorLayout?.parent?.requestLayout()
                }
            }

获取屏幕高度百分比的方法

 private fun getPopupHeight(percent: Float): Int {
        val displayMetrics = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
        return (displayMetrics.heightPixels * percent).toInt()
    }

【讨论】:

    【解决方案7】:

    Kotlin 中的解决方案受到Nandish A 的帖子的启发,更详细。首先是布局:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/container_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/bottom_sheet"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:behavior_hideable="false"
                app:behavior_peekHeight="0dp"
                app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
                      <!-- content of the bottom sheet -->
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    然后把它放到你的BottomSheetDialogFragment:

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
            // ...        
    
            dialog.setOnShowListener {
    
                val root = dialog.find<CoordinatorLayout>(R.id.container_root)
                val bottomSheetInternal = root.find<ConstraintLayout>(R.id.bottom_sheet)
    
                val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal)
                bottomSheetBehavior.isHideable = false
                BottomSheetBehavior.from(root.parent as View).peekHeight = root.height
                bottomSheetBehavior.peekHeight = root.height
                root.parent.requestLayout()
            }
    
            // ...
        }
    

    【讨论】:

      【解决方案8】:

      这对我来说是个窍门!我的班级扩展了 BottomSheetDialogFragment

      @Override
      public void onStart()
      {
          super.onStart();
          Dialog dialog = getDialog();
      
          if (dialog != null)
          {
              View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
              bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
          }
          View view = getView();
          view.post(() -> {
              View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
              View parent = (View) view.getParent();
              CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
              CoordinatorLayout.Behavior behavior = params.getBehavior();
              BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
              bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
              ((View) bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT);
          });
      }
      
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          View view = inflater.inflate(R.layout.screen_delivery_type, container, false);
      
          getDialog().setOnShowListener(new DialogInterface.OnShowListener()
          {
              @Override
              public void onShow(DialogInterface dialog)
              {
                  BottomSheetDialog d = (BottomSheetDialog) dialog;
                  FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.bottom_sheet);
                  CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
                  BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
                  bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
                  bottomSheetBehavior.setFitToContents(true);
                  bottomSheetBehavior.setExpandedOffset(0);
                  bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                  coordinatorLayout.getParent().requestLayout();
              }
          });
      }
      

      【讨论】:

        【解决方案9】:

        此代码是答案的组合。因为其中一些对我不起作用。

        在 XML ids.xml 中:

        <item name="sheet_parent_container" type="id" />
        

        在 XML 布局中:

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@id/sheet_parent_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">
        
            **Dialog content here!**
        
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        

        在 BottomSheetDialogFragment.kt 中:

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
        
            ...
        
            setPeekHeight(view)
        }
        
        /**
         * Function for disable half height display after screen rotation.
         */
        private fun setPeekHeight(view: View) {
            val parentContainer = view.findViewById<CoordinatorLayout>(R.id.sheet_parent_container)
        
            dialog?.setOnShowListener {
                val dialogParent = parentContainer.parent as View
                BottomSheetBehavior.from(dialogParent).peekHeight = parentContainer.height
                dialogParent.requestLayout()
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-18
          • 2020-11-23
          • 2013-05-10
          • 2016-08-15
          • 2016-06-26
          相关资源
          最近更新 更多