2017-05-17 85 views
0

我有簡單的Cardview奇怪的行爲。 當我運行我的應用程序時,一切都很好 - 正如我在第一張照片上顯示的那樣。 當我向下滾動,並再次scrollup每個Cardview元素更改高度,看起來像圖片2.任何想法是什麼原因導致此問題? pic1Android Cardview奇怪的行爲

pic2

而且我的xml: 內容滾動:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="flex.com.sklepik.MainActivity" 
    tools:showIn="@layout/main_activity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipToPadding="false" 
     android:scrollbars="vertical" /> 


</RelativeLayout> 

MainActivity:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="flex.com.sklepik.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/app_bar_height" 
     android:fitsSystemWindows="true" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/AppTheme.PopupOverlay"/> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_scrolling"/> 


</android.support.design.widget.CoordinatorLayout> 

單卡:

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

    <android.support.v7.widget.CardView 

     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_margin="@dimen/card_margin" 
     android:elevation="5dp" 
     card_view:cardCornerRadius="@dimen/card_shop_radius"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <ImageView 
       android:id="@+id/thumbnail" 
       android:layout_width="match_parent" 
       android:layout_height="160dp" 
       android:background="?attr/selectableItemBackgroundBorderless" 
       android:clickable="true" 
       android:scaleType="fitXY"/> 


     </RelativeLayout> 


    </android.support.v7.widget.CardView> 

</LinearLayout> 
+0

recyclerview項目根視圖的高度,在這種情況下'LinearLayout'不應該是match_parent。設定一個固定值。 – Saurabh

+0

是的,它解決了我的問題。你能回答@沙拉布嗎?我會標記它。無論如何,非常感謝 –

+0

或者,您可以爲LinearLayout,CardView和RelativeLayout設置android:layout_height =「wrap_content」,而不是硬編碼固定值。 –

回答

0

recyclerview項目根視圖的高度,在這種情況下LinearLayout不應該是match_parent。設定一個固定值。