NestedScrollView 设置固定屏幕尺寸

NestedScrollView set fixed screen size

开始吧 :) 我得到了 NestedScrollViewCollapsedToolbar。在这个 NSV 中,我有一个带有两个 RecyclerView 的 LinearLayout。接下来的问题是,我无法为这两个回收站设置固定大小,我不需要 NSV 滚动 => 我需要 NSV 高度 = [屏幕尺寸] - [折叠的工具栏高度]。这就是为什么我的回收商显示所有项目,但我需要屏幕高度的一半。

<AppBarLayout ... />
<android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">



                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/recycler_asks"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:scrollbars="none"
                            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                            app:layout_constraintBottom_toTopOf="@+id/divider"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            android:layout_marginBottom="@dimen/small_margin"
                            android:layout_above="@+id/divider"
                            android:background="@color/red"/>

                        <View
                            android:id="@+id/divider"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"/>

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/recycler_bids"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:orientation="vertical"
                            android:scrollbars="none"
                            app:layout_constraintTop_toBottomOf="@+id/divider"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                            android:layout_below="@+id/divider"
                            android:layout_marginTop="@dimen/small_margin"
                            android:background="@color/cyan"/>

                    </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

我该如何解决这个问题?

永远不要在scrollable中使用scrollable。 1. 创建 1 个具有不同类型视图的回收器视图。 2.加上layout_behavior.

输出

试试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scrollbars="none"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_asks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:scrollbars="none"
            android:layout_weight="1"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_constraintBottom_toTopOf="@+id/divider"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginBottom="@dimen/small_margin"
            android:layout_above="@+id/divider"
            android:background="@color/colorPrimary"/>

        <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

        <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_bids"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="none"
        android:layout_weight="1"
        app:layout_constraintTop_toBottomOf="@+id/divider"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        android:layout_below="@+id/divider"
        android:layout_marginTop="@dimen/small_margin"
        android:background="@color/colorAccent"/>

    </LinearLayout>


</android.support.v4.widget.NestedScrollView>

我发现的唯一方法是像下面的代码一样以编程方式制作它:

private void setContainerSize() {
    LinearLayout  contentContainer = getViewDataBinding().container;

    Point point =  SystemUtils.getScreenSize(this);
    float px = SystemUtils.convertDpToPixel(getResources().getDimension(R.dimen.toolbar_size), this);
    assert point != null;
    ViewGroup.LayoutParams layoutParams = contentContainer.getLayoutParams();
    layoutParams.height = (int)(point.getHeight() - px);
    contentContainer.setLayoutParams(layoutParams);
    contentContainer.requestLayout();
}