2016-07-27 49 views
1

我有一個簡單的設置:一個RecyclerView顯示項目列表,每個項目包含幾個TextView s。其中一個文本視圖可以有多行,最初我的android:maxLines設置爲1.在項目的點擊監聽器中,我在最多1行和最大無限(顯示完整內容)之間切換。它工作正常,除非它不是動畫。我試圖將android:animateLayoutChanges添加到我的視圖層次結構中的不同級別,但仍然沒有動畫。android:animateLayoutChanges在改變TextView的maxLines時不能在RecyclerView中工作

請讓我知道你的想法。

注意:我可以編程手動動畫,但我希望Android使用此xml屬性爲我處理它。


我的一些碼的:

我的列表項佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootCard" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:animateLayoutChanges="true" 
     android:padding="10dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp"> 

      <TextView 
       android:id="@+id/destination" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_marginEnd="36dp" 
       android:layout_marginRight="36dp" 
       android:textStyle="bold" 
       android:ellipsize="end" 
       android:maxLines="1" 
       android:textSize="25sp" /> 

      <ImageButton 
       android:id="@+id/editButton" 
       android:layout_width="24dp" 
       android:layout_height="24dp" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentTop="true" 
       android:src="@drawable/ic_edit" 
       android:contentDescription="@string/edit_button_content_descr" 
       android:background="?attr/selectableItemBackgroundBorderless" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/dateRange" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="17sp" /> 

      <Space 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="1"/> 

      <TextView 
       android:id="@+id/daysLeft" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginStart="20dp" 
       android:background="@drawable/days_left_background" 
       android:gravity="end" 
       android:layout_gravity="end" 
       android:textColor="@android:color/white" 
       android:textSize="15sp" 
       android:textStyle="bold"/> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/comments" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:textSize="15sp" 
      android:ellipsize="end" 
      android:maxLines="1" /> 
    </LinearLayout> 

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

在我看來持有者我擴大comments的TextView:

rootCard.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      comments.setMaxLines((comments.getMaxLines() == 1) ? Integer.MAX_VALUE : 1); 
     } 
    }); 

這裏是包含Recycler的片段佈局查看:

<?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="match_parent" 
    android:paddingBottom="@dimen/common_vertical_margin" 
    android:paddingLeft="@dimen/common_horizontal_margin" 
    android:paddingRight="@dimen/common_horizontal_margin" 
    android:paddingTop="@dimen/common_vertical_margin" 
    xmlns:tools="http://schemas.android.com/tools"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/tripList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:listitem="@layout/list_item_trip" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add" /> 

</RelativeLayout> 

回答

3

你沒有打電話給適配器的notifyItemChanged(),讓Android不知道的東西被改變,而應該是動畫。因此,所有你需要的是你改變你的maxLines屬性後添加此方法調用:

rootCard.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     comments.setMaxLines((comments.getMaxLines() == 1) ? Integer.MAX_VALUE : 1); 
     notifyItemChanged(viewHolder.getAdapterPosition()); 
    } 
}); 

如果它不會工作嘗試設置animateLayoutChanges佈局的根視圖 - 在你的情況下,它CardView

+0

謝謝,它的工作! – frangulyan

相關問題