am 2d719d6c: AnimatedRecyclerActivity demo supports simpler RV item animations

* commit '2d719d6c716e4b800c9b9bcd31fe36fdddb8adfd':
  AnimatedRecyclerActivity demo supports simpler RV item animations
This commit is contained in:
Chet Haase
2014-06-11 12:41:38 +00:00
committed by Android Git Automerger
3 changed files with 63 additions and 38 deletions

View File

@@ -6,12 +6,26 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<CheckBox <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enableAnimations" android:orientation="horizontal"
android:checked="true" android:layout_width="match_parent"
android:text="@string/enableAnimations" android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <CheckBox
android:id="@+id/enableAnimations"
android:checked="true"
android:text="@string/enableAnimations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/enablePredictiveAnimations"
android:checked="true"
android:text="@string/enablePredictiveAnimations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:orientation="horizontal"

View File

@@ -103,8 +103,9 @@
<string name="checkbox_layout_dir">Layout Dir</string> <string name="checkbox_layout_dir">Layout Dir</string>
<string name="checkbox_stack_from_end">Stack From End</string> <string name="checkbox_stack_from_end">Stack From End</string>
<string name="enableAnimations">Animate Changes</string> <string name="enableAnimations">Animate Changes</string>
<string name="enablePredictiveAnimations">Predictive Animations</string>
<string name="add_item">Add</string> <string name="add_item">Add</string>
<string name="delete_item">Delete</string> <string name="delete_item">Del</string>
<string name="add_delete_item">A+D</string> <string name="add_delete_item">A+D</string>
<string name="delete_add_item">D+A</string> <string name="delete_add_item">D+A</string>
<string name="d1a2d3">d1a2d3</string> <string name="d1a2d3">d1a2d3</string>

View File

@@ -45,9 +45,8 @@ public class AnimatedRecyclerView extends Activity {
ArrayList<String> mItems = new ArrayList<String>(); ArrayList<String> mItems = new ArrayList<String>();
MyAdapter mAdapter; MyAdapter mAdapter;
static final boolean USE_CUSTOM_ANIMATIONS = false;
boolean mAnimationsEnabled = true; boolean mAnimationsEnabled = true;
boolean mPredictiveAnimationsEnabled = true;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -74,6 +73,15 @@ public class AnimatedRecyclerView extends Activity {
mAnimationsEnabled = isChecked; 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 @Override
@@ -158,8 +166,8 @@ public class AnimatedRecyclerView extends Activity {
} }
@Override @Override
public boolean supportsItemAnimations() { public boolean supportsPredictiveItemAnimations() {
return mAnimationsEnabled; return mPredictiveAnimationsEnabled;
} }
@Override @Override
@@ -186,44 +194,46 @@ public class AnimatedRecyclerView extends Activity {
View v = recycler.getViewForPosition(mFirstPosition + i); View v = recycler.getViewForPosition(mFirstPosition + i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) v.getLayoutParams(); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) v.getLayoutParams();
if (!params.isItemRemoved()) { if (!mPredictiveAnimationsEnabled || !params.isItemRemoved()) {
addView(v); addView(v);
} }
measureChild(v, 0, 0); measureChild(v, 0, 0);
bottom = top + v.getMeasuredHeight(); bottom = top + v.getMeasuredHeight();
v.layout(left, top, right, bottom); v.layout(left, top, right, bottom);
if (params.isItemRemoved()) { if (mPredictiveAnimationsEnabled && params.isItemRemoved()) {
parentBottom += v.getHeight(); parentBottom += v.getHeight();
} }
} }
// Now that we've run a full layout, figure out which views were not used if (mAnimationsEnabled && mPredictiveAnimationsEnabled) {
// (cached in previousViews). For each of these views, position it where // Now that we've run a full layout, figure out which views were not used
// it would go, according to its position relative to the visible // (cached in previousViews). For each of these views, position it where
// positions in the list. This information will be used by RecyclerView to // it would go, according to its position relative to the visible
// record post-layout positions of these items for the purposes of animating them // positions in the list. This information will be used by RecyclerView to
// out of view // record post-layout positions of these items for the purposes of animating them
// out of view
View lastVisibleView = getChildAt(getChildCount() - 1); View lastVisibleView = getChildAt(getChildCount() - 1);
if (lastVisibleView != null) { if (lastVisibleView != null) {
RecyclerView.LayoutParams lastParams = RecyclerView.LayoutParams lastParams =
(RecyclerView.LayoutParams) lastVisibleView.getLayoutParams(); (RecyclerView.LayoutParams) lastVisibleView.getLayoutParams();
int lastPosition = lastParams.getViewPosition(); int lastPosition = lastParams.getViewPosition();
final List<RecyclerView.ViewHolder> previousViews = recycler.getScrapList(); final List<RecyclerView.ViewHolder> previousViews = recycler.getScrapList();
count = previousViews.size(); count = previousViews.size();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
View view = previousViews.get(i).itemView; View view = previousViews.get(i).itemView;
RecyclerView.LayoutParams params = RecyclerView.LayoutParams params =
(RecyclerView.LayoutParams) view.getLayoutParams(); (RecyclerView.LayoutParams) view.getLayoutParams();
int position = params.getViewPosition(); int position = params.getViewPosition();
int newTop; int newTop;
if (position < mFirstPosition) { if (position < mFirstPosition) {
newTop = view.getHeight() * (position - mFirstPosition); newTop = view.getHeight() * (position - mFirstPosition);
} else { } else {
newTop = lastVisibleView.getTop() + view.getHeight() * newTop = lastVisibleView.getTop() + view.getHeight() *
(position - lastPosition); (position - lastPosition);
}
view.offsetTopAndBottom(newTop - view.getTop());
} }
view.offsetTopAndBottom(newTop - view.getTop());
} }
} }
} }