2017-03-02 7 views
0

RecyclerViewアイテムをアニメーションしようとしましたが、その矩形をゼロ幅から100%(MATCH_PARENT)に拡大して項目の背景になるとアニメーション化しようとしています。0dp幅からMATCH_PARENTへのビューをアニメ化

しかし、私はアニメーションが動作するのを見ることができません。つまり、最初の背景は白ですが、長方形は灰色であるため、クリックされたアイテムは灰色になります。しかし、これは起こっていない。

ここでは、アイテムのxmlです:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 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="wrap_content" 
    app:cardCornerRadius="0dp" 
    android:focusable="true" 
    android:clickable="true"> 

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

     <View 
      android:id="@+id/colored_bar" 
      android:layout_width="3dp" 
      android:layout_height="match_parent" 
      android:background="@drawable/colored_bar_bg1"></View> 

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

      <View 
       android:id="@+id/option_background_container" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:background="#e0e0e0"></View> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:paddingBottom="16dp" 
       android:paddingTop="16dp"> 

       <ImageView 
        android:id="@+id/icon" 
        android:layout_width="48dp" 
        android:layout_height="48dp" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_alignParentTop="true" 
        android:layout_marginLeft="13dp" 
        app:srcCompat="@drawable/ic_lock" /> 

       <TextView 
        android:id="@+id/card_title" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:layout_toEndOf="@+id/icon" 
        android:layout_toRightOf="@+id/icon" 
        android:paddingBottom="16dp" 
        android:paddingLeft="8dp" 
        android:textColor="?android:attr/textColorPrimary" 
        android:textSize="16sp" 
        tools:text="@string/option_title_label" /> 

       <TextView 
        android:id="@+id/card_subtitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBottom="@+id/card_title" 
        android:layout_toEndOf="@+id/icon" 
        android:layout_toRightOf="@+id/icon" 
        android:paddingLeft="8dp" 
        android:textSize="14sp" 
        tools:text="@string/option_description_label" /> 
      </RelativeLayout> 
     </FrameLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

やアニメーションにするコード:それは働いていないのはなぜ

public class OptionListItemHolder extends RecyclerView.ViewHolder { 

     private TextView cardTitle; 
     private TextView cardSubtitle; 
     private ImageView icon; 
     private View coloredBar; 
     private View optionBackground; 

     public OptionListItemHolder(View v) { 
      super(v); 
      cardTitle = (TextView)v.findViewById(R.id.card_title); 
      cardSubtitle = (TextView)v.findViewById(R.id.card_subtitle); 
      icon = (ImageView)v.findViewById(R.id.icon); 
      coloredBar = v.findViewById(R.id.colored_bar); 
      optionBackground = v.findViewById(R.id.option_background_container); 

      v.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        ObjectAnimator animation = ObjectAnimator.ofInt(optionBackground, "width", 0, view.getWidth()); 
        animation.setDuration(600); 
        animation.setInterpolator(new DecelerateInterpolator()); 

        animation.start(); 
       } 
      }); 
     } 
    } 

は?

+0

で親の幅を取得することができます

ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), newWidth); widthAnimator.setDuration(500); widthAnimator.setInterpolator(new DecelerateInterpolator()); widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { view.getLayoutParams().width = (int) animation.getAnimatedValue(); view.requestLayout(); } }); widthAnimator.start(); 

機能することを発見しましたview.getWidth()? – 0X0nosugar

+0

返される値は720です。しかし、どこから来たのかを知りたい場合は、リサイクラのビューアイテムコンテナです。 –

答えて

1

私はValueAnimatorを使用する代わりに、ビューの幅が親を一致させる必要がある場合、あなたは値が何であるか

int parentWidth = ((View)view.getParent()).getMeasuredWidth(); 
+0

それは動作します!ありがとうございました。 – susemi99

関連する問題