Skip to content

Commit bb487b3

Browse files
committed
Allow to display an icon, if a list item does not contain an ImageView.
1 parent c22da49 commit bb487b3

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

library/src/main/java/de/mrapp/android/dialog/adapter/ArrayRecyclerViewAdapter.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import androidx.recyclerview.widget.RecyclerView;
3434

3535
import de.mrapp.android.dialog.R;
36+
import de.mrapp.android.util.ThemeUtil;
3637
import de.mrapp.util.Condition;
3738

3839
/**
@@ -163,18 +164,43 @@ private void adaptIcon(@NonNull final ViewHolder holder, final int position) {
163164

164165
if (imageView != null) {
165166
Context context = imageView.getContext();
166-
Drawable icon = ActivityCompat.getDrawable(context, iconResourceIds[position]);
167-
168-
if (icon != null) {
169-
DrawableCompat.setTintList(icon, itemTintList);
170-
DrawableCompat.setTintMode(icon, itemTintMode);
167+
imageView.setImageDrawable(getIcon(context, position));
168+
} else {
169+
TextView textView = holder.textView;
170+
171+
if (textView != null) {
172+
Context context = textView.getContext();
173+
int drawablePadding = ThemeUtil.getDimensionPixelSize(context,
174+
android.R.attr.listPreferredItemPaddingLeft);
175+
textView.setCompoundDrawablePadding(drawablePadding);
176+
textView.setCompoundDrawablesWithIntrinsicBounds(getIcon(context, position),
177+
null, null, null);
171178
}
172-
173-
imageView.setImageDrawable(icon);
174179
}
175180
}
176181
}
177182

183+
/**
184+
* Returns the icon of a list item.
185+
*
186+
* @param context The context, which should be used, as an instance of the class
187+
* {@link Context}. The context may not be null
188+
* @param position The position of the list item as an {@link Integer} value
189+
* @return The icon of the list item as an instance of the class {@link Drawable} or null, if
190+
* the list item does not have an icon
191+
*/
192+
@Nullable
193+
private Drawable getIcon(@NonNull final Context context, final int position) {
194+
Drawable icon = ActivityCompat.getDrawable(context, iconResourceIds[position]);
195+
196+
if (icon != null) {
197+
DrawableCompat.setTintList(icon, itemTintList);
198+
DrawableCompat.setTintMode(icon, itemTintMode);
199+
}
200+
201+
return icon;
202+
}
203+
178204
/**
179205
* Creates a new recycler view adapter, which displays list items that correspond to the texts
180206
* that are contained in an array.

0 commit comments

Comments
 (0)