如何设置底表窥视高度最初只显示某些部分?

Dam*_*les 3 android android-widget android-layout bottom-sheet

假设我的“底部工作表”中包含以下几行小部件。如果我想只显示前两行(即前两行LinearLayout),而不显示下面的其余小部件。我不希望这些内容一开始就被看到。如何设置正确的窥视高度?硬编码app:behavior_peekHeight可能无法工作,因此我需要以编程方式进行设置,但是如何计算高度呢?

还是有更推荐的方法来获得相同的结果?我的意思是,如果我测试Google Maps,长按位置会首先仅将标题部分显示为底部,但是当我尝试向上滚动底部时,感觉好像标题部分(可能不是真正的标题部分)底页)替换为包含所有元素的真实底页。如果我的解释还不够,请自己尝试使用Google地图。

<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>
Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 9

我可以通过使用a ViewTreeObserver.OnGlobalLayoutListener等待底部的页面布局,然后BottomSheetBehavior.setPeekHeight()使用您不想看到的第一个视图的y坐标进行调用来解决此问题。

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());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我的底页是NestedScrollView一个LinearLayout可容纳许多TextViews的容纳物。通过将窥视高度设置为第三个高度TextView(由获得getChildAt(2)),我的底部工作表收合时恰好显示了2 TextViews。

在此处输入图片说明 在此处输入图片说明


AL.*_*AL. 5

定制了 @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
                    }
                })
    }
}
Run Code Online (Sandbox Code Playgroud)

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