【问题标题】:How to set Bottom Sheet peek height to show only certain parts initially?如何设置底部表窥视高度以最初仅显示某些部分?
【发布时间】:2018-08-07 10:14:08
【问题描述】:

假设我的底部工作表有如下几行小部件。如果我最初只想显示前两行(即前两行LinearLayouts),而不是下面的其余小部件。我不希望最初看到那些。如何设置正确的窥视高度?硬编码app:behavior_peekHeight 可能不起作用,所以我需要以编程方式设置它,但是如何计算高度?

或者有没有更推荐的方法来获得相同的结果?我的意思是,如果我测试谷歌地图,长按一个位置首先只显示标题部分作为底部表,但是当我尝试向上滚动底部表时,感觉好像标题部分(这可能不是真实的bottom sheet) 被替换为包含所有元素的真实底部表。如果我的解释还不够,请自行尝试谷歌地图。

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView/>
            <android.support.v7.widget.AppCompatSpinner/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView/>
            <TextView/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView/>
            <TextView/>
        </LinearLayout>
        <android.support.v7.widget.RecyclerView/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【问题讨论】:

    标签: android android-layout android-widget bottom-sheet


    【解决方案1】:

    我会通过使用ViewTreeObserver.OnGlobalLayoutListener 来等待您的底部工作表布局好,然后使用您不想看到的第一个视图的 y 坐标调用 BottomSheetBehavior.setPeekHeight() 来解决这个问题。

    private BottomSheetBehavior<View> behavior;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        View bottomSheet = findViewById(R.id.bottomSheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
    
        final LinearLayout inner = findViewById(R.id.inner);
        inner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                inner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                View hidden = inner.getChildAt(2);
                behavior.setPeekHeight(hidden.getTop());
            }
        });
    }
    

    在这种情况下,我的底部工作表是一个NestedScrollView,其中包含一个LinearLayout,其中包含许多TextViews。通过将窥视高度设置为第三个TextView 的顶部(由getChildAt(2) 获得),我的底部工作表最终会在折叠时恰好显示两个TextViews。

    【讨论】:

    • 我可以获取此代码的布局文件吗?如果可能的话,代码也可以。我正在尝试将 peekheight 设置为与我拖动底页的位置相同。但我找不到当前拖动位置的值。
    【解决方案2】:

    自定义@Ben P. 的答案以将视图 id 定位为peekHeight 的引用并制作了一个函数:

    /**
     * Gets the bottom part of the target view and sets it as the peek height of the specified @{BottomSheetBehavior}
     *
     * @param layout - layout of the bottom sheet.
     * @param targetViewId - id of the target view. Must be a view inside the 'layout' param.
     * @param behavior - bottom sheet behavior recipient.
     */
    private fun <T : ViewGroup> getViewBottomHeight(layout: ViewGroup,
                                                    targetViewId: Int,
                                                    behavior: BottomSheetBehavior<T>) {
        layout.apply {
            viewTreeObserver.addOnGlobalLayoutListener(
                    object : ViewTreeObserver.OnGlobalLayoutListener {
                        override fun onGlobalLayout() {
                            viewTreeObserver.removeOnGlobalLayoutListener(this)
                            behavior.peekHeight = findViewById<View>(targetViewId).bottom
                        }
                    })
        }
    }
    

    在我们的用例中,我们需要定位视图的底部部分,所以我们这样设置。它可以根据用例进行调整。

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 2011-03-28
      • 2021-11-02
      • 2016-06-01
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多