Android - 动画后添加子视图

时间:2015-12-08 00:08:39

标签: android android-layout animation

我想在动画后尝试在另一个视图下面添加一个子视图。任何人都知道我做错了什么?

this.animate().translationY(newY - 170).setDuration(500).withEndAction(new Runnable() {
        @Override
        public void run() {
            clearAnimation();

            RouteBreakDownLayout routeBreakDownLayout = new RouteBreakDownLayout(mContext);
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            p.addRule(RelativeLayout.BELOW, R.id.selected_route_layout);
            routeBreakDownLayout.setLayoutParams(p);
            addView(routeBreakDownLayout);

        }
    }).start();

这是布局的XML。我有一个嵌入的scrollview,它将保存需要动画的内容。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected_route_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/selected_route_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="#FFF"
        android:gravity="bottom"
        android:padding="10dip">

        <TextView
            android:id="@+id/time_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#1D1C18" />

        <TextView
            android:id="@+id/eta_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/time_label"
            android:layout_toEndOf="@+id/time_label"
            android:layout_toRightOf="@+id/time_label"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#7EC82F" />

        <LinearLayout
            android:id="@+id/route_breakdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/time_label"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="10dip"
            android:gravity="center_vertical"
            android:orientation="horizontal" />

        <TextView
            android:id="@+id/via_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/route_breakdown"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#BDBDBD" />

        <ImageButton
            android:id="@+id/arrow_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:padding="15dip"
            android:src="@drawable/transit_symbol_up_arrow" />

    </RelativeLayout>

    <ScrollView
        android:layout_below="@+id/selected_route_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:showDividers="middle"
            android:divider="?android:dividerHorizontal"
            android:animateLayoutChanges="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

    </ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你的代码片段出了什么问题?

这是我提出的工作示例:

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

    <View
        android:id="@+id/headerView"
        android:background="#00FF00"
        android:layout_width="match_parent"
        android:layout_height="25dp"/>

    <View
        android:id="@+id/detailsView"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:clickable="true"
        android:background="#FF0000"/>

</RelativeLayout>

MainActivity是

package klogi.com.animationstestapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View detailsView = findViewById(R.id.detailsView);
        final View headerView = findViewById(R.id.headerView);
        final RelativeLayout container = (RelativeLayout)findViewById(R.id.containerRelativeLayout);
        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detailsView.animate().translationY(-detailsView.getY()).setDuration(500).withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        Button additionalButton = new Button(MainActivity.this);
                        additionalButton.setText("Additional button");

                        RelativeLayout.LayoutParams additionalButtonParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        additionalButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        additionalButton.setLayoutParams(additionalButtonParams);

                        container.addView(additionalButton);
                    }
                }).start();

                headerView.animate().translationY(detailsView.getHeight()).setDuration(500).start();
            }
        });
    }
}

它的效果与预期一致。

您是否尝试添加控件(RouteBreakDownLayout)没有动画?它有用吗?它可能像控件实际上正在添加,但它不在视口中,等等 - 到目前为止,我只能猜测。