@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // Skip this work if a transition is running; it sets the scroll values independently // and should not have those animated values clobbered by this logic LayoutTransition transition = mLinearLayout.getLayoutTransition(); if (transition != null && transition.isRunning()) { return; } // Keep track of the last visible item in the list so we can restore it // to the bottom when the orientation changes. mLastScrollPosition = scrollPositionOfMostRecent(); // This has to happen post-layout, so run it "in the future" post( new Runnable() { public void run() { // Make sure we're still not clobbering the transition-set values, since this // runnable launches asynchronously LayoutTransition transition = mLinearLayout.getLayoutTransition(); if (transition == null || !transition.isRunning()) { scrollTo(mLastScrollPosition, 0); } } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout_animation); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); rootContainer = (LinearLayout) findViewById(R.id.root_container); // rootContainer.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // String text = "test view " + (index ++); // TextView textView = new TextView(LayoutAnimationActivity.this); // textView.setId(View.generateViewId()); // textView.setText(text); // LinearLayout.LayoutParams layoutParams = new // LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, // ViewGroup.LayoutParams.WRAP_CONTENT); // layoutParams.setMargins(10, 10, 10, 10); // rootContainer.addView(textView); // lastView = textView; // } // }); fab.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String text = "test view " + (index++); TextView textView = new TextView(LayoutAnimationActivity.this); textView.setId(View.generateViewId()); textView.setText(text); textView.setAlpha(0f); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(10, 10, 10, 10); rootContainer.addView(textView); lastView = textView; } }); getSupportActionBar().setDisplayHomeAsUpEnabled(true); rootContainer.getLayoutTransition().setAnimateParentHierarchy(false); ObjectAnimator appearingAnimator1 = new ObjectAnimator(); appearingAnimator1.setPropertyName("x"); appearingAnimator1.setFloatValues(rootContainer.getPaddingLeft(), 200); appearingAnimator1.setDuration(1000); ObjectAnimator appearingAnimator2 = new ObjectAnimator(); appearingAnimator2.setPropertyName("x"); appearingAnimator2.setFloatValues(200, rootContainer.getPaddingLeft()); appearingAnimator2.setDuration(1000); ObjectAnimator appearingAnimator3 = new ObjectAnimator(); appearingAnimator3.setPropertyName("alpha"); appearingAnimator3.setFloatValues(0f, 1f); appearingAnimator3.setDuration(4000); AnimatorSet set = new AnimatorSet(); set.playTogether(appearingAnimator1, appearingAnimator3); set.play(appearingAnimator2).after(appearingAnimator1); rootContainer.getLayoutTransition().setAnimator(LayoutTransition.APPEARING, set); }