【问题标题】:RecyclerView within ScrollView wrap_content height not working as intendedScrollView wrap_content 高度中的 RecyclerView 未按预期工作
【发布时间】:2017-01-15 18:00:43
【问题描述】:

我在ScrollView 中嵌入了一个RecyclerView。这是完整的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:orientation="vertical">

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

            <fragment
                android:name="devarshi.sample.view.ProductPortfolioFragment"
                android:id="@+id/fragmentProductPortfolio"
                android:layout_width="match_parent"
                android:layout_margin="10dp"
                android:layout_height="230dp" />

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

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textViewProductName" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textViewProductDescription" />

                <LinearLayout
                    android:id="@+id/linearLayoutProductDetails"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:text="@string/product_detail_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/textViewProductDetails" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:id="@+id/linearyLayoutOtherProductDetails"
                android:layout_height="100dp">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="0dp"
                    android:layout_weight="0.6"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:id="@+id/linearLayoutPrice"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/textViewProductPrice"
                            android:layout_width="wrap_content"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:textColor="@color/colorDiscountedPrice"
                            android:layout_height="wrap_content"
                            android:text="1500"/>

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewOldProductPrice"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="200"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/textViewQuantity"
                            android:layout_width="wrap_content"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:textColor="@color/colorDiscountedPrice"
                            android:layout_height="wrap_content"
                            android:text="Quantity"/>

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewQuantityValue"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="200"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/textViewBrand"
                            android:layout_width="wrap_content"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:textColor="@color/colorDiscountedPrice"
                            android:layout_height="wrap_content"
                            android:text="Brand"/>

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewBrandValue"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="200"/>
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="0dp"
                    android:layout_weight="0.4"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:id="@+id/linearLayoutFreeShipping"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            app:srcCompat="@drawable/product_detail_free_shipping_icon"
                            android:id="@+id/imageViewFreeShipping" />

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewFreeShipping"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="@string/product_detail_free_shipping"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:id="@+id/linearLayoutCashOnDelivery"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            app:srcCompat="@drawable/product_detail_cash_on_delivery_icon"
                            android:id="@+id/imageViewCashOnDelivery" />

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewCashOnDelivery"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="@string/product_detail_cash_on_delivery"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:id="@+id/linearLayoutUnit"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_marginLeft="5dp"
                            android:id="@+id/textViewUnitLabel"
                            android:layout_width="wrap_content"
                            android:textSize="14sp"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="wrap_content"
                            android:text="@string/product_detail_unit"/>

                        <Button
                            android:text="-"
                            android:layout_width="@dimen/product_detail_counter_button_width"
                            android:layout_height="@dimen/product_detail_counter_button_height"
                            android:id="@+id/buttonDecrement" />

                        <TextView
                            android:id="@+id/textViewUnitValue"
                            android:layout_width="27dp"
                            android:textSize="14sp"
                            android:gravity="center"
                            android:textColor="@color/colorActualPrice"
                            android:layout_height="@dimen/product_detail_counter_button_height"
                            android:text="0"/>

                        <Button
                            android:text="+"
                            android:layout_width="@dimen/product_detail_counter_button_width"
                            android:layout_height="@dimen/product_detail_counter_button_height"
                            android:id="@+id/buttonIncrement" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewProductDetails"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

问题是——

虽然我已将RecyclerViewlayout_height 设置为wrap_content,但它的高度部分包裹了内容,即理想情况下在wrap_content 之后,它的高度应该是150dp,但它目前是70dp

另一个有趣的场景是——

如果我将linearyLayoutOtherProductDetails 移动到recyclerViewProductDetails 下方,那么它会将RecyclerView 高度准确地显示为150dp。似乎当 RecyclerView 加载到布局上时,它正在根据布局上当前可见的项目数(其中)计算高度,即高度等于 5 个项目而不是 9 个项目。

关于如何让RecyclerView 在第一种情况下完全包裹其内容的任何想法?

注意:我使用的是com.android.support:recyclerview-v7:25.0.0

【问题讨论】:

    标签: java android android-recyclerview android-scrollview


    【解决方案1】:

    ScrollView 中使用RecyclerView 并不是一个很好的做法。您可以考虑将它们全部保存在 NestedScrollView 中,这可能有助于您的目的。这是documentation from developers android

    在您的情况下,您可能会考虑将linearyLayoutOtherProductDetails 作为RecyclerView 的标题,这应该是最好的解决方案。摆脱ScrollViewNestedScrollView 并将自定义布局添加为RecyclerView 的页眉或页脚。

    查看my answer here,了解如何在RecyclerView 中添加页眉/页脚。

    【讨论】:

    • 你救了我 2 次 =) 谢谢。
    • 很高兴知道这一点!编码愉快!
    【解决方案2】:

    我遇到了同样的问题,最后我发现RecyclerView和LinearLayoutManager的测量逻辑可能在使用UNSPECIFIED模式的heightMeasureSpec而不是零大小时存在错误。

    我已经将此bug报告给google issuetracker,你可以查看详情。

    NestedScrollView 也可能在它的 child 的垂直边距不为零时导致同样的问题,因为它可能会将 UNSPECIFIED 模式的 heightMeasureSpec 而不是零 size 传递给它的 child,然后 child 可能会将类似的 heightMeasureSpec 传递给 RecyclerView。 NestedScrollView的相关代码:

    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
    
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
    
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
    

    要修复这个bug,你可以简单地使用下面继承的RecyclerView:

    public class FixRecyclerView extends RecyclerView {
    
        public FixRecyclerView(@NonNull Context context) {
            super(context);
        }
    
        public FixRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public FixRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthSpec, int heightSpec) {
            if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED && MeasureSpec.getSize(heightSpec) != 0) {
                super.onMeasure(widthSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            } else {
                super.onMeasure(widthSpec, heightSpec);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      制作recyclerview的linearlayoutOtherProductdetails header(第一项)并移除scrollview。因为另一个滚动视图中的滚动视图无法正常工作。 另一种解决方案是使用自定义布局管理器,它将回收器视图的高度设置为其内容的高度。但这可能会导致滚动效果不佳。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多