2017-01-15 1 views
1

ScrollView에 삽입되어 있습니다. 여기에 전체 레이아웃입니다 :ScrollView 내의 RecyclerView wrap_content의 높이가 의도 한대로 작동하지 않습니다.

<?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> 

문제는 -

높이가 부분적으로 높이가 150dp해야 즉, 이상적으로 wrap_content 후 내용을 포장한다 wrap_content 나는이 RecyclerViewlayout_height을 설정 한 비록하지만 현재 중입니다 70dp.

enter image description here

또 다른 흥미로운 시나리오는 -

그때 그것을 보여줍니다 linearyLayoutOtherProductDetailsrecyclerViewProductDetails 아래로 이동하면 RecyclerView 높이를 정확하게 150dp한다. RecyclerView이 레이아웃에로드 될 때처럼 레이아웃에서 현재 볼 수있는 항목 수를 기준으로 높이를 계산하는 것처럼 보입니다. 즉, 높이는 9 개 항목 대신 5 개의 항목과 같습니다.

enter image description here

방법 높이의 RecyclerView가 완전히 첫 번째 시나리오의 내용을 포장 내가 한 수에 어떤 아이디어?

참고 : 내가 com.android.support:recyclerview-v7:25.0.0

답변

4

RecyclerViewScrollView 안에 사용하는 것은 좋지 않습니다. 귀하의 목적을 달성 할 수있는 NestedScrollView 안에 모든 것을 보관하는 것이 좋습니다. documentation from developers android은 다음과 같습니다.

귀하의 경우 RecyclerView의 헤더로 linearyLayoutOtherProductDetails을 고려하는 것이 가장 좋은 해결책입니다. ScrollView 또는 NestedScrollView을 제거하고 사용자 정의 레이아웃을 RecyclerView의 머리글 또는 바닥 글로 추가하십시오.

RecyclerView에 머리글/바닥 글을 추가하는 방법은 my answer here을 확인하십시오.

1

이 recyclerview의 linearlayoutOtherProductdetails 헤더 (첫 번째 항목)를 확인하고있는 ScrollView를 제거 사용하고 있습니다. 다른 스크롤보기 내의 스크롤보기가 제대로 작동하지 않기 때문입니다. 또 다른 해결책은 사용자 지정 레이아웃 관리자를 사용하여 리사이클 뷰의 높이를 내용의 높이와 같게 설정하는 것입니다. 그러나 이로 인해 스크롤이 불량해질 수 있습니다.

관련 문제