2017-04-14 16 views
0

我有一個recyclerview使用自定義適配器和自定義項目佈局。 我需要知道將高程應用於recyclerview的標準是什麼。 是要應用於recyclerview本身或項目佈局的標高。仰角和statelistanimator不與recyclerview列表項目

我還需要定義statelistanimator以在選擇項目時啓用高程更改。 我應該在哪裏設置statelistanimator,recyclerview或項目佈局?

我嘗試了幾種方法來實現這一點。但是當我運行應用程序時沒有任何工作。 當我將高程和statelistanimator設置爲項目佈局時,我可以在設計時看到高程效果。但是當我運行該應用程序時,recyclerview中的項目列表中不顯示任何內容。

如果我做錯了什麼或不遵守標準,請幫助我。

這裏是我的源我試過,

資源\佈局\ list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:elevation="4dp" 
    android:stateListAnimator="@animator/state_list" 
    android:background="#3F51B5" 
    android:padding="5dp" 
    android:clipToPadding="false"> 


      <TextView 
       android:id="@+id/tv_item" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sample Item" 
       android:textSize="18sp" 
       android:textColor="#ffffff" 
       android:padding="5dp"> 

      </TextView> 



</RelativeLayout> 

資源\佈局\ activity_main.xml中

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

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

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



</RelativeLayout> 

資源\ animator \ state_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <objectAnimator 
      android:propertyName="translationZ" 
      android:valueTo="8dp" 
      android:valueType="floatType"> 

     </objectAnimator> 
    </item> 

    <item android:state_pressed="false"> 
     <objectAnimator 
      android:propertyName="translationZ" 
      android:valueTo="4dp" 
      android:valueType="floatType"> 

     </objectAnimator> 

    </item> 
</selector> 
+0

我認爲你正在尋找這個。 http://stackoverflow.com/a/30746596/7704356 –

+0

@Harisali:是的。但不需要創建鏈接中提供的所有東西。 Android本身使用Android L及以上版本的材質設計標準提供海拔,陰影和動畫。 我遵循材料設計概念。但是儘管我做了上面提到的所有這些事情,但它並不工作。 –

回答

0

爲什麼只是不使用cardview?

<android.support.v7.widget.CardView android:id="@+id/item_card" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    app:cardCornerRadius="2dp" 
    app:cardUseCompatPadding="true" 
    app:cardElevation="2dp" 
    app:cardBackgroundColor="@color/md_white_1000" 
    android:foreground="?android:attr/selectableItemBackground" 
    android:clickable="true"> 

     <TextView 
       android:id="@+id/tv_item" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sample Item" 
       android:textSize="18sp" 
       android:textColor="#ffffff" 
       android:padding="5dp"> 

      </TextView> 
</android.support.v7.widget.CardView> 
+0

感謝您的回覆。但我需要知道如何使用現有代碼實現相同。應該有一些標準來實現這一點。 –

0

使用cardview作爲RecyclerviewList項目。 Card View example

它有app:cardElevation屬性,可以給你想要的效果。