translationY视图动画跳到最后

时间:2015-03-02 14:55:21

标签: android animation android-view android-recyclerview

我正在尝试创建一个动画,其中显示了一个布局,当我按下它时,另一个会在它下面消失。

因为我想把它放在recyclerView中我希望分隔符在第二个布局显示之前向下滑动。

我遇到的问题是分频器没有动画向下移动,它只是直接跳下来。

这是我的代码:

package com.example.itayrabin.layouttest;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class rvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>     implements View.OnClickListener {

private Context mContext;

public rvAdapter(Context context) {
    mContext = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(mContext).inflate(R.layout.layout,parent,false);
    final TestViewHolder holder = new TestViewHolder(view);
    holder.llContent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.llExpendedContent.setVisibility(View.INVISIBLE);

            holder.vDivider.animate()
                    .translationY(holder.llExpendedContent.getMeasuredHeight())
                    .setDuration(500)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);

                            holder.llExpendedContent.setVisibility(View.VISIBLE);
                        }
                    }).start();
        }
    });
    return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}

@Override
public int getItemCount() {
    return 20;
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.llContent){

    }
}


public static class TestViewHolder extends RecyclerView.ViewHolder {


    @InjectView(R.id.llExpendedContent)
    LinearLayout llExpendedContent;
    @InjectView(R.id.vDivider)
    View vDivider;
    @InjectView(R.id.llContent)
    LinearLayout llContent;

    public TestViewHolder(View view) {
        super(view);

        ButterKnife.inject(this,view);

    }

}
}

这是一行的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical" android:id="@+id/llContent" android:clickable="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/llMainContent"
    android:layout_weight="1">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Bread(5)"
        android:textColor="@color/abc_primary_text_material_light"
        android:textStyle="bold" />

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="15.3 Dollars"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/llExpendedContent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:visibility="gone">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Milk(10)"
        android:textColor="@color/abc_primary_text_material_light"
        android:textStyle="bold" />

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="60 Dollars"/>

    </LinearLayout>

<View
    android:id="@+id/vDivider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#eeeeee" />

(你可以看到我使用的是ButterKnife,因为它易于使用。)

在动画的持续时间之后第二个布局确实会出现,所以它不像动画没有播放什么,只是它直接跳下来。

1 个答案:

答案 0 :(得分:0)

您是否尝试使用y(float)?也; start()是多余的。

        holder.vDivider.animate()
                .y(holder.llExpendedContent.getMeasuredHeight())
                .setDuration(500)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);

                        holder.llExpendedContent.setVisibility(View.VISIBLE);
                    }
                });