Merge "Add RecyclerView animations for change actions"
This commit is contained in:
@@ -25,6 +25,13 @@
|
|||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"/>
|
android:layout_height="wrap_content"/>
|
||||||
|
|
||||||
|
<CheckBox
|
||||||
|
android:id="@+id/enableChangeAnimations"
|
||||||
|
android:checked="false"
|
||||||
|
android:text="@string/enableChangeAnimations"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
<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"
|
||||||
|
android:onClick="itemClicked"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
@@ -11,9 +12,20 @@
|
|||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"/>
|
android:layout_height="wrap_content"/>
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:id="@+id/text"
|
android:orientation="vertical"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"/>
|
android:layout_height="wrap_content">
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/text"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/expandedText"
|
||||||
|
android:visibility="gone"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -102,8 +102,9 @@
|
|||||||
<string name="checkbox_reverse">Rev.</string>
|
<string name="checkbox_reverse">Rev.</string>
|
||||||
<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</string>
|
||||||
<string name="enablePredictiveAnimations">Predictive Animations</string>
|
<string name="enablePredictiveAnimations">Predictive</string>
|
||||||
|
<string name="enableChangeAnimations">Change Anims</string>
|
||||||
<string name="add_item">Add</string>
|
<string name="add_item">Add</string>
|
||||||
<string name="delete_item">Del</string>
|
<string name="delete_item">Del</string>
|
||||||
<string name="add_delete_item">A+D</string>
|
<string name="add_delete_item">A+D</string>
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.example.android.supportv7.widget;
|
package com.example.android.supportv7.widget;
|
||||||
|
|
||||||
|
import android.support.v4.util.ArrayMap;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import com.example.android.supportv7.R;
|
import com.example.android.supportv7.R;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
@@ -47,6 +48,7 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
|
|
||||||
boolean mAnimationsEnabled = true;
|
boolean mAnimationsEnabled = true;
|
||||||
boolean mPredictiveAnimationsEnabled = true;
|
boolean mPredictiveAnimationsEnabled = true;
|
||||||
|
RecyclerView.ItemAnimator mCachedAnimator = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -55,6 +57,7 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
|
|
||||||
ViewGroup container = (ViewGroup) findViewById(R.id.container);
|
ViewGroup container = (ViewGroup) findViewById(R.id.container);
|
||||||
mRecyclerView = new RecyclerView(this);
|
mRecyclerView = new RecyclerView(this);
|
||||||
|
mCachedAnimator = mRecyclerView.getItemAnimator();
|
||||||
mRecyclerView.setLayoutManager(new MyLayoutManager(this));
|
mRecyclerView.setLayoutManager(new MyLayoutManager(this));
|
||||||
mRecyclerView.setHasFixedSize(true);
|
mRecyclerView.setHasFixedSize(true);
|
||||||
mRecyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
mRecyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
@@ -70,6 +73,11 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
enableAnimations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
enableAnimations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||||
|
if (isChecked && mRecyclerView.getItemAnimator() == null) {
|
||||||
|
mRecyclerView.setItemAnimator(mCachedAnimator);
|
||||||
|
} else if (!isChecked && mRecyclerView.getItemAnimator() != null) {
|
||||||
|
mRecyclerView.setItemAnimator(null);
|
||||||
|
}
|
||||||
mAnimationsEnabled = isChecked;
|
mAnimationsEnabled = isChecked;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -82,6 +90,15 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
mPredictiveAnimationsEnabled = isChecked;
|
mPredictiveAnimationsEnabled = isChecked;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
CheckBox enableChangeAnimations =
|
||||||
|
(CheckBox) findViewById(R.id.enableChangeAnimations);
|
||||||
|
enableChangeAnimations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||||
|
mCachedAnimator.setSupportsChangeAnimations(isChecked);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -104,6 +121,13 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
mAdapter.selectItem(holder, selected);
|
mAdapter.selectItem(holder, selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void itemClicked(View view) {
|
||||||
|
ViewGroup parent = (ViewGroup) view;
|
||||||
|
MyViewHolder holder = (MyViewHolder) mRecyclerView.getChildViewHolder(parent);
|
||||||
|
mAdapter.toggleExpanded(holder);
|
||||||
|
mAdapter.notifyItemChanged(holder.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
public void deleteItem(View view) {
|
public void deleteItem(View view) {
|
||||||
int numItems = mItems.size();
|
int numItems = mItems.size();
|
||||||
if (numItems > 0) {
|
if (numItems > 0) {
|
||||||
@@ -135,6 +159,7 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
private void addAtPosition(int position, String text) {
|
private void addAtPosition(int position, String text) {
|
||||||
mItems.add(position, text);
|
mItems.add(position, text);
|
||||||
mAdapter.mSelected.put(text, Boolean.FALSE);
|
mAdapter.mSelected.put(text, Boolean.FALSE);
|
||||||
|
mAdapter.mExpanded.put(text, Boolean.FALSE);
|
||||||
mAdapter.notifyItemInserted(position);
|
mAdapter.notifyItemInserted(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,7 +424,8 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
class MyAdapter extends RecyclerView.Adapter {
|
class MyAdapter extends RecyclerView.Adapter {
|
||||||
private int mBackground;
|
private int mBackground;
|
||||||
List<String> mData;
|
List<String> mData;
|
||||||
HashMap<String, Boolean> mSelected = new HashMap<String, Boolean>();
|
ArrayMap<String, Boolean> mSelected = new ArrayMap<String, Boolean>();
|
||||||
|
ArrayMap<String, Boolean> mExpanded = new ArrayMap<String, Boolean>();
|
||||||
|
|
||||||
public MyAdapter(List<String> data) {
|
public MyAdapter(List<String> data) {
|
||||||
TypedValue val = new TypedValue();
|
TypedValue val = new TypedValue();
|
||||||
@@ -409,6 +435,7 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
mData = data;
|
mData = data;
|
||||||
for (String itemText : mData) {
|
for (String itemText : mData) {
|
||||||
mSelected.put(itemText, Boolean.FALSE);
|
mSelected.put(itemText, Boolean.FALSE);
|
||||||
|
mExpanded.put(itemText, Boolean.FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,11 +453,20 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||||
String itemText = mData.get(position);
|
String itemText = mData.get(position);
|
||||||
((MyViewHolder) holder).textView.setText(itemText);
|
((MyViewHolder) holder).textView.setText(itemText);
|
||||||
|
((MyViewHolder) holder).expandedText.setText("More text for the expanded version");
|
||||||
boolean selected = false;
|
boolean selected = false;
|
||||||
if (mSelected.get(itemText) != null) {
|
if (mSelected.get(itemText) != null) {
|
||||||
selected = mSelected.get(itemText);
|
selected = mSelected.get(itemText);
|
||||||
}
|
}
|
||||||
((MyViewHolder) holder).checkBox.setChecked(selected);
|
((MyViewHolder) holder).checkBox.setChecked(selected);
|
||||||
|
Boolean expanded = mExpanded.get(itemText);
|
||||||
|
if (expanded != null && expanded) {
|
||||||
|
((MyViewHolder) holder).expandedText.setVisibility(View.VISIBLE);
|
||||||
|
((MyViewHolder) holder).textView.setVisibility(View.GONE);
|
||||||
|
} else {
|
||||||
|
((MyViewHolder) holder).expandedText.setVisibility(View.GONE);
|
||||||
|
((MyViewHolder) holder).textView.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -445,14 +481,21 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
public void selectItem(MyViewHolder holder, boolean selected) {
|
public void selectItem(MyViewHolder holder, boolean selected) {
|
||||||
mSelected.put((String) holder.textView.getText(), selected);
|
mSelected.put((String) holder.textView.getText(), selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void toggleExpanded(MyViewHolder holder) {
|
||||||
|
String text = (String) holder.textView.getText();
|
||||||
|
mExpanded.put(text, !mExpanded.get(text));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class MyViewHolder extends RecyclerView.ViewHolder {
|
static class MyViewHolder extends RecyclerView.ViewHolder {
|
||||||
|
public TextView expandedText;
|
||||||
public TextView textView;
|
public TextView textView;
|
||||||
public CheckBox checkBox;
|
public CheckBox checkBox;
|
||||||
|
|
||||||
public MyViewHolder(View v) {
|
public MyViewHolder(View v) {
|
||||||
super(v);
|
super(v);
|
||||||
|
expandedText = (TextView) v.findViewById(R.id.expandedText);
|
||||||
textView = (TextView) v.findViewById(R.id.text);
|
textView = (TextView) v.findViewById(R.id.text);
|
||||||
checkBox = (CheckBox) v.findViewById(R.id.selected);
|
checkBox = (CheckBox) v.findViewById(R.id.selected);
|
||||||
}
|
}
|
||||||
@@ -461,4 +504,5 @@ public class AnimatedRecyclerView extends Activity {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString() + " \"" + textView.getText() + "\"";
|
return super.toString() + " \"" + textView.getText() + "\"";
|
||||||
}
|
}
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user