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
This commit is contained in:
parent
c8fc4211a7
commit
bb9086d69d
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
19
library/recyclerview/res/drawable/item_bg.xml
Normal file
19
library/recyclerview/res/drawable/item_bg.xml
Normal file
|
@ -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>
|
20
library/recyclerview/res/layout/test_existing_background.xml
Normal file
20
library/recyclerview/res/layout/test_existing_background.xml
Normal file
|
@ -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" />
|
|
@ -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."
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue