ListView动画。 2个动画中只有1个有效

时间:2014-11-12 10:00:41

标签: android listview animation

我有一个ListView,当我点击一个行项目标题时,它会展开(带动画)并在其下面显示更多信息。当再次单击行项目标题时,它应该播放另一个缩小它的动画。

扩展动画工作正常,但缩小动画根本不起作用。

为什么只有expandDownAnim有效而不是shrinkUpAnim

ListView行XML

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/horizontal_line" />

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/schedule_header_bg"
    android:padding="10dp" >

    <com.walintukai.lfdate.widgets.CustomTextView
        android:id="@+id/day"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:textSize="18sp"
        android:textColor="@color/secondary_blue" />

    <com.walintukai.lfdate.widgets.CustomTextView
        android:id="@+id/is_active"
        android:layout_width="34dp"
        android:layout_height="24dp"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:textColor="#fff"
        android:textSize="12sp"
        android:background="@drawable/bg_gray_radius" />

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/horizontal_line" />

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#fff"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentLeft="true" >

            <com.walintukai.lfdate.widgets.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:text="@string/start" />

            <com.walintukai.lfdate.widgets.CustomTextView
                android:id="@+id/start_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textStyle="bold"
                android:textSize="19sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentRight="true" >

            <com.walintukai.lfdate.widgets.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:text="@string/end" />

            <com.walintukai.lfdate.widgets.CustomTextView
                android:id="@+id/end_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textStyle="bold"
                android:textSize="19sp" />

        </LinearLayout>

    </RelativeLayout>

    <com.edmodo.rangebar.RangeBar
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rangebar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        custom:tickHeight="0dp"
        custom:tickCount="24" />

</LinearLayout>

具有动画(缩短)的BaseAdapter

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.row_schedule, null);
        holder.header = (LinearLayout) convertView.findViewById(R.id.header);
        holder.day = (CustomTextView) convertView.findViewById(R.id.day);
        holder.isActive = (CustomTextView) convertView.findViewById(R.id.is_active);
        holder.container = (LinearLayout) convertView.findViewById(R.id.container);
        holder.rangeBar = (RangeBar) convertView.findViewById(R.id.rangebar);
        holder.tvStartTime = (CustomTextView) convertView.findViewById(R.id.start_time);
        holder.tvEndTime = (CustomTextView) convertView.findViewById(R.id.end_time);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (mSchedules.get(position).getDate() != null && !mSchedules.get(position).getDate().isEmpty())
        holder.day.setText(getFormattedDate(mSchedules.get(position).getDate()));

    if (mSchedules.get(position).getIsActive()) {
        holder.isActive.setText(R.string.btn_on);
        holder.isActive.setBackgroundResource(R.drawable.bg_green_radius);
        holder.container.setVisibility(View.VISIBLE);
    }
    else {
        holder.isActive.setText(R.string.btn_off);
        holder.isActive.setBackgroundResource(R.drawable.bg_gray_radius);
        holder.container.setVisibility(View.GONE);
    }

    holder.header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mSchedules.get(position).getIsActive()) {
                mSchedules.get(position).setIsActive(false);

                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.shrink_up);
                anim.setDuration(300);

                holder.container.setVisibility(View.GONE);
                holder.container.setAnimation(anim);
                holder.container.animate();
                anim.start();

                holder.isActive.setText(R.string.btn_off);
                holder.isActive.setBackgroundResource(R.drawable.bg_gray_radius);
            }
            else {
                mSchedules.get(position).setIsActive(true);

                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.expand_down);
                anim.setDuration(300);

                holder.container.setVisibility(View.VISIBLE);
                holder.container.setAnimation(anim);
                holder.container.animate();
                anim.start();

                holder.isActive.setText(R.string.btn_on);
                holder.isActive.setBackgroundResource(R.drawable.bg_green_radius);
            }
        }
    });

    final DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("hh:mm a");

    int startTime = mSchedules.get(position).getStartTime();
    int endTime = mSchedules.get(position).getEndTime();

    LocalTime localStartTime = new LocalTime(startTime, 0, 0);
    LocalTime localEndTime = new LocalTime(endTime, 0, 0);

    holder.tvStartTime.setText(timeFormatter.print(localStartTime));
    holder.tvEndTime.setText(timeFormatter.print(localEndTime));
    holder.rangeBar.setThumbIndices(startTime, endTime);

    holder.rangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
        @Override
        public void onIndexChangeListener(RangeBar rangeBar, int leftThumbIndex, int rightThumbIndex) {
            mSchedules.get(position).setStartTime(leftThumbIndex);
            mSchedules.get(position).setEndTime(rightThumbIndex);

            LocalTime localStartTime = new LocalTime(leftThumbIndex, 0, 0);
            LocalTime localEndTime = new LocalTime(rightThumbIndex, 0, 0);

            holder.tvStartTime.setText(timeFormatter.print(localStartTime));
            holder.tvEndTime.setText(timeFormatter.print(localEndTime));
        }
    });

    return convertView;
}

更新1:添加了AnimationListener

为我的动画添加了AnimationListener,但现在当我点击关闭一行时,它会卡住,只有在我点击当前关闭的另一行时才会关闭。然后,所有应该关闭的行都将同时关闭(通过单击当前关闭的行触发)。

holder.header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mSchedules.get(position).getIsActive()) {
                mSchedules.get(position).setIsActive(false);

                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.shrink_up);
                anim.setDuration(300);
                anim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        holder.container.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });

                holder.container.setAnimation(anim);
                holder.container.animate();
                anim.start();

                holder.isActive.setText(R.string.btn_off);
                holder.isActive.setBackgroundResource(R.drawable.bg_gray_radius);
            }
            else {
                mSchedules.get(position).setIsActive(true);

                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.expand_down);
                anim.setDuration(300);

                holder.container.setVisibility(View.VISIBLE);
                holder.container.setAnimation(anim);
                holder.container.animate();
                anim.start();

                holder.isActive.setText(R.string.btn_on);
                holder.isActive.setBackgroundResource(R.drawable.bg_green_radius);
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

因为您在开始动画之前将可见性设置为 GONE 。 你必须改变:

            holder.container.setVisibility(View.GONE);
            holder.container.setAnimation(shrinkUpAnim);
            holder.container.animate();
            shrinkUpAnim.start();

并将可见性设置为GONE onAnimationEnd(),这是一个示例:

yourAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    holder.container.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });