ScrollView中的java RecyclerView wrap_内容高度未按预期工作
我在ScrollView
中嵌入了一个RecyclerView
。以下是完整的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<ScrollView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:fillViewport="true"
安卓:orientation="vertical">
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="vertical"
安卓:layout_height="wrap_content">
<fragment
安卓:name="devarshi.sample.view.ProductPortfolioFragment"
安卓:id="@+id/fragmentProductPortfolio"
安卓:layout_width="match_parent"
安卓:layout_margin="10dp"
安卓:layout_height="230dp" />
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<TextView
安卓:text="TextView"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/textViewProductName" />
<TextView
安卓:text="TextView"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/textViewProductDescription" />
<LinearLayout
安卓:id="@+id/linearLayoutProductDetails"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<TextView
安卓:text="@string/product_detail_title"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/textViewProductDetails" />
</LinearLayout>
</LinearLayout>
<LinearLayout
安卓:orientation="horizontal"
安卓:layout_width="match_parent"
安卓:id="@+id/linearyLayoutOtherProductDetails"
安卓:layout_height="100dp">
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="0dp"
安卓:layout_weight="0.6"
安卓:layout_height="wrap_content">
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:id="@+id/linearLayoutPrice"
安卓:layout_height="wrap_content">
<TextView
安卓:id="@+id/textViewProductPrice"
安卓:layout_width="wrap_content"
安卓:textSize="15sp"
安卓:textStyle="bold"
安卓:textColor="@color/colorDiscountedPrice"
安卓:layout_height="wrap_content"
安卓:text="1500"/>
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewOldProductPrice"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="200"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:layout_height="wrap_content">
<TextView
安卓:id="@+id/textViewQuantity"
安卓:layout_width="wrap_content"
安卓:textSize="15sp"
安卓:textStyle="bold"
安卓:textColor="@color/colorDiscountedPrice"
安卓:layout_height="wrap_content"
安卓:text="Quantity"/>
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewQuantityValue"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="200"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:layout_height="wrap_content">
<TextView
安卓:id="@+id/textViewBrand"
安卓:layout_width="wrap_content"
安卓:textSize="15sp"
安卓:textStyle="bold"
安卓:textColor="@color/colorDiscountedPrice"
安卓:layout_height="wrap_content"
安卓:text="Brand"/>
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewBrandValue"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="200"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="0dp"
安卓:layout_weight="0.4"
安卓:layout_height="wrap_content">
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:id="@+id/linearLayoutFreeShipping"
安卓:layout_height="wrap_content">
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
app:srcCompat="@drawable/product_detail_free_shipping_icon"
安卓:id="@+id/imageViewFreeShipping" />
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewFreeShipping"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="@string/product_detail_free_shipping"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:id="@+id/linearLayoutCashOnDelivery"
安卓:layout_height="wrap_content">
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
app:srcCompat="@drawable/product_detail_cash_on_delivery_icon"
安卓:id="@+id/imageViewCashOnDelivery" />
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewCashOnDelivery"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="@string/product_detail_cash_on_delivery"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:orientation="horizontal"
安卓:id="@+id/linearLayoutUnit"
安卓:layout_height="wrap_content">
<TextView
安卓:layout_marginLeft="5dp"
安卓:id="@+id/textViewUnitLabel"
安卓:layout_width="wrap_content"
安卓:textSize="14sp"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="wrap_content"
安卓:text="@string/product_detail_unit"/>
<Button
安卓:text="-"
安卓:layout_width="@dimen/product_detail_counter_button_width"
安卓:layout_height="@dimen/product_detail_counter_button_height"
安卓:id="@+id/buttonDecrement" />
<TextView
安卓:id="@+id/textViewUnitValue"
安卓:layout_width="27dp"
安卓:textSize="14sp"
安卓:gravity="center"
安卓:textColor="@color/colorActualPrice"
安卓:layout_height="@dimen/product_detail_counter_button_height"
安卓:text="0"/>
<Button
安卓:text="+"
安卓:layout_width="@dimen/product_detail_counter_button_width"
安卓:layout_height="@dimen/product_detail_counter_button_height"
安卓:id="@+id/buttonIncrement" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<安卓.support.v7.widget.RecyclerView
安卓:id="@+id/recyclerViewProductDetails"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
</安卓.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</LinearLayout>
问题是——
虽然我已经将layout_height
的RecyclerView
设置为wrap_content
,但它的高度部分地包裹了内容,也就是说,理想情况下在wrap_content
之后,它的高度应该是150dp
,但它目前是70dp
另一个有趣的场景是——
如果我把linearyLayoutOtherProductDetails
移到recyclerViewProductDetails
下面,那么它将RecyclerView
高度精确地显示为150dp
。似乎当RecyclerView
加载到布局上时,它是根据布局上当前可见的项目数(其中)计算高度的,即高度等于5个项目,而不是9个项目
在第一个场景中,我如何让RecyclerView
的高度完全包裹其内容,有什么想法吗
注意:我正在使用com.安卓.support:recyclerview-v7:25.0.0
# 1 楼答案
在
ScrollView
中使用RecyclerView
不是一个很好的实践。你可以考虑把它们放在一个^ {CD3}}内,这可能有你的用处。这是documentation from developers android在您的情况下,您可以考虑将{{CD4}}作为^ {}的标题,这应该是最好的解决方案。摆脱
ScrollView
或NestedScrollView
并添加自定义布局作为RecyclerView
的页眉或页脚查看my answer here,了解如何在^{中添加页眉/页脚