From bb9086d69daf97f22eb096ecb6055b8360958ac7 Mon Sep 17 00:00:00 2001 From: Ajay Nadathur Date: Fri, 14 Apr 2017 15:02:58 -0700 Subject: [PATCH] Customize Item callbacks and support existing background in RecycleItemAdapter - Added support for custom backgrounds in RecyclerItemAdapter. - Added callbacks in Item to allow overriding state/level that gets copied from icon to drawable Test: Tests added, verified that changes work Change-Id: I33c883e0a1a0b76ccccaf8a61ab0bb49f46da3fb --- .../android/setupwizardlib/items/Item.java | 13 ++++++++++-- library/recyclerview/res/drawable/item_bg.xml | 19 ++++++++++++++++++ .../res/layout/test_existing_background.xml | 20 +++++++++++++++++++ .../items/RecyclerItemAdapter.java | 7 +++++-- .../items/RecyclerItemAdapterTest.java | 17 +++++++++++++++- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 library/recyclerview/res/drawable/item_bg.xml create mode 100644 library/recyclerview/res/layout/test_existing_background.xml diff --git a/library/main/src/com/android/setupwizardlib/items/Item.java b/library/main/src/com/android/setupwizardlib/items/Item.java index 59ab1a1..fc8823e 100644 --- a/library/main/src/com/android/setupwizardlib/items/Item.java +++ b/library/main/src/com/android/setupwizardlib/items/Item.java @@ -155,8 +155,7 @@ public class Item extends AbstractItem { // Set the image drawable to null before setting the state and level to avoid affecting // any recycled drawable in the ImageView iconView.setImageDrawable(null); - iconView.setImageState(icon.getState(), false /* merge */); - iconView.setImageLevel(icon.getLevel()); + onMergeIconStateAndLevels(iconView, icon); iconView.setImageDrawable(icon); iconContainer.setVisibility(View.VISIBLE); } else { @@ -165,4 +164,14 @@ public class Item extends AbstractItem { view.setId(getViewId()); } + + /** + * Copies state and level information from {@link #getIcon()} to the currently bound view's + * ImageView. Subclasses can override this method to change whats being copied from the icon + * to the ImageView. + */ + protected void onMergeIconStateAndLevels(ImageView iconView, Drawable icon) { + iconView.setImageState(icon.getState(), false /* merge */); + iconView.setImageLevel(icon.getLevel()); + } } diff --git a/library/recyclerview/res/drawable/item_bg.xml b/library/recyclerview/res/drawable/item_bg.xml new file mode 100644 index 0000000..285ae28 --- /dev/null +++ b/library/recyclerview/res/drawable/item_bg.xml @@ -0,0 +1,19 @@ + + + + + \ No newline at end of file diff --git a/library/recyclerview/res/layout/test_existing_background.xml b/library/recyclerview/res/layout/test_existing_background.xml new file mode 100644 index 0000000..fa2b22a --- /dev/null +++ b/library/recyclerview/res/layout/test_existing_background.xml @@ -0,0 +1,20 @@ + + + diff --git a/library/recyclerview/src/com/android/setupwizardlib/items/RecyclerItemAdapter.java b/library/recyclerview/src/com/android/setupwizardlib/items/RecyclerItemAdapter.java index a676c60..78280a6 100644 --- a/library/recyclerview/src/com/android/setupwizardlib/items/RecyclerItemAdapter.java +++ b/library/recyclerview/src/com/android/setupwizardlib/items/RecyclerItemAdapter.java @@ -109,8 +109,11 @@ public class RecyclerItemAdapter extends RecyclerView.Adapter R.styleable.SuwRecyclerItemAdapter_selectableItemBackground); } - final Drawable background = typedArray.getDrawable( - R.styleable.SuwRecyclerItemAdapter_android_colorBackground); + Drawable background = view.getBackground(); + if (background == null) { + background = typedArray.getDrawable( + R.styleable.SuwRecyclerItemAdapter_android_colorBackground); + } if (selectableItemBackground == null || background == null) { Log.e(TAG, "Cannot resolve required attributes." diff --git a/library/recyclerview/test/instrumentation/src/com/android/setupwizardlib/items/RecyclerItemAdapterTest.java b/library/recyclerview/test/instrumentation/src/com/android/setupwizardlib/items/RecyclerItemAdapterTest.java index 3867bfe..1bfbb95 100644 --- a/library/recyclerview/test/instrumentation/src/com/android/setupwizardlib/items/RecyclerItemAdapterTest.java +++ b/library/recyclerview/test/instrumentation/src/com/android/setupwizardlib/items/RecyclerItemAdapterTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.verify; import android.graphics.Rect; import android.graphics.drawable.Drawable; +import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.RectShape; import android.support.test.InstrumentationRegistry; @@ -133,7 +134,7 @@ public class RecyclerItemAdapterTest { } @Test - public void testCreateViewHolderNoBcakground() { + public void testCreateViewHolderNoBackground() { RecyclerItemAdapter adapter = new RecyclerItemAdapter(mItemGroup); FrameLayout parent = new FrameLayout(InstrumentationRegistry.getContext()); @@ -141,4 +142,18 @@ public class RecyclerItemAdapterTest { adapter.onCreateViewHolder(parent, R.layout.test_list_item_no_background); assertNull("Background should be null", viewHolder.itemView.getBackground()); } + + @Test + public void testCreateViewHolderWithExistingBackground() { + RecyclerItemAdapter adapter = new RecyclerItemAdapter(mItemGroup); + FrameLayout parent = new FrameLayout(InstrumentationRegistry.getContext()); + + final ItemViewHolder viewHolder = + adapter.onCreateViewHolder(parent, R.layout.test_existing_background); + Drawable background = viewHolder.itemView.getBackground(); + assertTrue(background instanceof PatchedLayerDrawable); + + PatchedLayerDrawable layerDrawable = (PatchedLayerDrawable) background; + assertTrue(layerDrawable.getDrawable(0) instanceof GradientDrawable); + } }