They discover animateLayoutChanges this property used to implement layout update the animation, which is pre-loaded each time the layout to make the animation run after the change, by increasing animateLayoutChanges = "true" to the layout, you can easily complete.
First, to achieve
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/shadow_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:text="添加新View"
android:onClick="onAddViewClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearLayout"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
import android.animation.LayoutTransition;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private int mCount=1;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout= findViewById(R.id.linearLayout);
}
public void onAddViewClick(View view) {
TextView textView = new TextView(this);
textView.setText("newItem:"+mCount++);
ViewGroup.MarginLayoutParams params =new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,130);
params.bottomMargin=10;
textView.setLayoutParams(params);
textView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
textView.setTextColor(Color.WHITE);
textView.setGravity(Gravity.CENTER|Gravity.LEFT);
mLinearLayout.addView(textView,0);
}
}
Is not it simple?
Second, the use in the ListView
Also provided in the ListView android: animateLayoutChanges = "true".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/shadow_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:text="添加新View"
android:onClick="onAddViewClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<ListView
android:id="@+id/listview"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Here with a very simple array adapter
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView mListView;
private ArrayAdapter<String> mArrayAdapter;
private List<String > mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView= findViewById(R.id.listview);
mDatas = new ArrayList<>();
mArrayAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);
mListView.setAdapter(mArrayAdapter);
}
public void onAddViewClick(View view) {
mDatas.add(0,"newItem"+(mDatas.size()+1));
mArrayAdapter.notifyDataSetChanged();
}
}
Three, LayoutTransition
Since it can be set by xml, of course, the code can be set up, it will naturally use LayoutTransition class.
Animation may be added or deleted by ViewGroup.setLayoutTransition ViewGroup disposed on the sub-View, as in this example, and is provided in a second animation to be completed.
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(1000);
mListView.setLayoutTransition(layoutTransition);
You can also customize the animation, such as setting a right-to-left into effect
LayoutTransition layoutTransition = new LayoutTransition();
ObjectAnimator animator = ObjectAnimator.ofFloat(mListView, "translationX", 200, 0);
layoutTransition.setAnimator(LayoutTransition.APPEARING, animator);
mListView.setLayoutTransition(layoutTransition);
LayoutTransition There are 5 types of animation:
1.APPEARING: animation running on a new view container appears.
2.CHANGE_APPEARING: As the new view appears containers, other passive to change the view to run animation own position.
3.CHANGE_DISAPPEARING: Due to disappear from the view of a container and the other to change their view passive position of running animation.
4.CHANGING: running on the view animations when layout change, but change is not due to the layout view to add or remove the container from the container caused.
5.DISAPPEARING: running in the container on the disappearance of view animation.
Examples of the above APPEARING is animated, there is another one the same setDuration a reload mode, means for setting the long type.