java - ScrollView wrap_content 高度内的 RecyclerView 未按预期工作

标签 java android android-recyclerview android-scrollview

我在 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

enter image description here

另一个有趣的场景是——

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

enter image description here

关于如何在第一种情况下让 RecyclerView 完全包裹其内容有什么想法吗?

注意:我正在使用 com.android.support:recyclerview-v7:25.0.0

最佳答案

ScrollView 中使用 RecyclerView 并不是一个很好的做法。您可能会考虑将它们全部放在 NestedScrollView 中,这可能会满足您的目的。这是 documentation from developers android .

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

检查 my answer here关于如何在 RecyclerView 中添加页眉/页脚。

关于java - ScrollView wrap_content 高度内的 RecyclerView 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41664419/

相关文章:

java - 是否需要双重定义数据类型?

android - 向下滚动时 RecyclerView 项目之间的间隙较大

java - Dialog 内的 RecyclerView 底部被截断

android - ViewPager 中的 RecyclerView 中的 ExpandableListView

java - Infinispan JGROUPS 强制更改 View

java - 如何在 Firebase 数据库 (Android) 中计算子项数量

java - 如何避免读取空值时强制关闭

android - 在三星手机上设置应用程序的设备所有者权限

java - 具有 <ui :repeat> 的不同 div 标签 id

java - 如何从一个jpanel到另一个jpanel画一条线