diff --git a/samples/Support7Demos/res/layout/animated_recycler_view.xml b/samples/Support7Demos/res/layout/animated_recycler_view.xml index 188a1548b..8fed40cd5 100644 --- a/samples/Support7Demos/res/layout/animated_recycler_view.xml +++ b/samples/Support7Demos/res/layout/animated_recycler_view.xml @@ -6,12 +6,26 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + + + + + + + Layout Dir Stack From End Animate Changes + Predictive Animations Add - Delete + Del A+D D+A d1a2d3 diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/AnimatedRecyclerView.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/AnimatedRecyclerView.java index 83cf4d3b0..6d8f7203c 100644 --- a/samples/Support7Demos/src/com/example/android/supportv7/widget/AnimatedRecyclerView.java +++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/AnimatedRecyclerView.java @@ -45,9 +45,8 @@ public class AnimatedRecyclerView extends Activity { ArrayList mItems = new ArrayList(); MyAdapter mAdapter; - static final boolean USE_CUSTOM_ANIMATIONS = false; - boolean mAnimationsEnabled = true; + boolean mPredictiveAnimationsEnabled = true; @Override protected void onCreate(Bundle savedInstanceState) { @@ -74,6 +73,15 @@ public class AnimatedRecyclerView extends Activity { mAnimationsEnabled = isChecked; } }); + + CheckBox enablePredictiveAnimations = + (CheckBox) findViewById(R.id.enablePredictiveAnimations); + enablePredictiveAnimations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + mPredictiveAnimationsEnabled = isChecked; + } + }); } @Override @@ -158,8 +166,8 @@ public class AnimatedRecyclerView extends Activity { } @Override - public boolean supportsItemAnimations() { - return mAnimationsEnabled; + public boolean supportsPredictiveItemAnimations() { + return mPredictiveAnimationsEnabled; } @Override @@ -186,44 +194,46 @@ public class AnimatedRecyclerView extends Activity { View v = recycler.getViewForPosition(mFirstPosition + i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) v.getLayoutParams(); - if (!params.isItemRemoved()) { + if (!mPredictiveAnimationsEnabled || !params.isItemRemoved()) { addView(v); } measureChild(v, 0, 0); bottom = top + v.getMeasuredHeight(); v.layout(left, top, right, bottom); - if (params.isItemRemoved()) { + if (mPredictiveAnimationsEnabled && params.isItemRemoved()) { parentBottom += v.getHeight(); } } - // Now that we've run a full layout, figure out which views were not used - // (cached in previousViews). For each of these views, position it where - // it would go, according to its position relative to the visible - // positions in the list. This information will be used by RecyclerView to - // record post-layout positions of these items for the purposes of animating them - // out of view + if (mAnimationsEnabled && mPredictiveAnimationsEnabled) { + // Now that we've run a full layout, figure out which views were not used + // (cached in previousViews). For each of these views, position it where + // it would go, according to its position relative to the visible + // positions in the list. This information will be used by RecyclerView to + // record post-layout positions of these items for the purposes of animating them + // out of view - View lastVisibleView = getChildAt(getChildCount() - 1); - if (lastVisibleView != null) { - RecyclerView.LayoutParams lastParams = - (RecyclerView.LayoutParams) lastVisibleView.getLayoutParams(); - int lastPosition = lastParams.getViewPosition(); - final List previousViews = recycler.getScrapList(); - count = previousViews.size(); - for (int i = 0; i < count; ++i) { - View view = previousViews.get(i).itemView; - RecyclerView.LayoutParams params = - (RecyclerView.LayoutParams) view.getLayoutParams(); - int position = params.getViewPosition(); - int newTop; - if (position < mFirstPosition) { - newTop = view.getHeight() * (position - mFirstPosition); - } else { - newTop = lastVisibleView.getTop() + view.getHeight() * - (position - lastPosition); + View lastVisibleView = getChildAt(getChildCount() - 1); + if (lastVisibleView != null) { + RecyclerView.LayoutParams lastParams = + (RecyclerView.LayoutParams) lastVisibleView.getLayoutParams(); + int lastPosition = lastParams.getViewPosition(); + final List previousViews = recycler.getScrapList(); + count = previousViews.size(); + for (int i = 0; i < count; ++i) { + View view = previousViews.get(i).itemView; + RecyclerView.LayoutParams params = + (RecyclerView.LayoutParams) view.getLayoutParams(); + int position = params.getViewPosition(); + int newTop; + if (position < mFirstPosition) { + newTop = view.getHeight() * (position - mFirstPosition); + } else { + newTop = lastVisibleView.getTop() + view.getHeight() * + (position - lastPosition); + } + view.offsetTopAndBottom(newTop - view.getTop()); } - view.offsetTopAndBottom(newTop - view.getTop()); } } }