am 2d719d6c: AnimatedRecyclerActivity demo supports simpler RV item animations
* commit '2d719d6c716e4b800c9b9bcd31fe36fdddb8adfd': AnimatedRecyclerActivity demo supports simpler RV item animations
This commit is contained in:
@@ -6,12 +6,26 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/enableAnimations"
|
||||
android:checked="true"
|
||||
android:text="@string/enableAnimations"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
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"
|
||||
android:orientation="horizontal"
|
||||
|
||||
@@ -103,8 +103,9 @@
|
||||
<string name="checkbox_layout_dir">Layout Dir</string>
|
||||
<string name="checkbox_stack_from_end">Stack From End</string>
|
||||
<string name="enableAnimations">Animate Changes</string>
|
||||
<string name="enablePredictiveAnimations">Predictive Animations</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="delete_add_item">D+A</string>
|
||||
<string name="d1a2d3">d1a2d3</string>
|
||||
|
||||
@@ -45,9 +45,8 @@ public class AnimatedRecyclerView extends Activity {
|
||||
ArrayList<String> mItems = new ArrayList<String>();
|
||||
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<RecyclerView.ViewHolder> 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<RecyclerView.ViewHolder> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user