diff options
author | Ajay Nadathur <ajayns@google.com> | 2017-04-14 15:02:58 -0700 |
---|---|---|
committer | Ajay Nadathur <ajayns@google.com> | 2017-04-18 10:33:22 -0700 |
commit | bb9086d69daf97f22eb096ecb6055b8360958ac7 (patch) | |
tree | 7b8c37616b1cab81b6977d863393a95f06b01ad7 /library | |
parent | c8fc4211a744cb947c732d959ef34bf10d48798c (diff) | |
download | setupwizard-bb9086d69daf97f22eb096ecb6055b8360958ac7.tar.gz |
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
Diffstat (limited to 'library')
5 files changed, 71 insertions, 5 deletions
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 @@ +<!-- + Copyright (C) 2015 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <corners android:radius="1dp"/> +</shape>
\ 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 @@ +<!-- + Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@drawable/item_bg" /> 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<ItemViewHolder> 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); + } } |