|
33 | 33 | import androidx.recyclerview.widget.RecyclerView; |
34 | 34 |
|
35 | 35 | import de.mrapp.android.dialog.R; |
| 36 | +import de.mrapp.android.util.ThemeUtil; |
36 | 37 | import de.mrapp.util.Condition; |
37 | 38 |
|
38 | 39 | /** |
@@ -163,18 +164,43 @@ private void adaptIcon(@NonNull final ViewHolder holder, final int position) { |
163 | 164 |
|
164 | 165 | if (imageView != null) { |
165 | 166 | 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); |
171 | 178 | } |
172 | | - |
173 | | - imageView.setImageDrawable(icon); |
174 | 179 | } |
175 | 180 | } |
176 | 181 | } |
177 | 182 |
|
| 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 | + |
178 | 204 | /** |
179 | 205 | * Creates a new recycler view adapter, which displays list items that correspond to the texts |
180 | 206 | * that are contained in an array. |
|
0 commit comments