|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
4 | 4 | import android.graphics.drawable.Drawable;
|
| 5 | +import android.support.v7.widget.RecyclerView; |
| 6 | +import android.view.LayoutInflater; |
5 | 7 | import android.view.View;
|
6 | 8 | import android.view.ViewGroup;
|
7 |
| -import android.widget.ArrayAdapter; |
8 | 9 | import android.widget.TextView;
|
9 | 10 |
|
| 11 | +import java.util.ArrayList; |
10 | 12 | import java.util.List;
|
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | * A simple array adapter that additionally provide an icon per entry.
|
14 | 16 | */
|
15 |
| -class ArrayAdapterWithIcons extends ArrayAdapter<ArrayAdapterWithIcons.Item> { |
16 |
| - public final List<Item> items; |
| 17 | +class ArrayAdapterWithIcons extends RecyclerView.Adapter<ArrayAdapterWithIcons.ViewHolder> { |
| 18 | + final static int resource = android.R.layout.select_dialog_item; |
| 19 | + final static int textViewResourceId = android.R.id.text1; |
| 20 | + public final List<Item> items = new ArrayList<>(); |
| 21 | + private int dp5; |
17 | 22 |
|
18 | 23 | @SuppressWarnings("SameParameterValue")
|
19 |
| - public ArrayAdapterWithIcons(Context context, int resource, int textViewResourceId, List<Item> objects) { |
20 |
| - super(context, resource, textViewResourceId, objects); |
21 |
| - items = objects; |
| 24 | + public ArrayAdapterWithIcons(Context context) { |
| 25 | + //Add margin between image and text (support various screen densities) |
| 26 | + dp5 = (int) (5 * context.getResources().getDisplayMetrics().density + 0.5f); |
22 | 27 | }
|
23 | 28 |
|
24 | 29 | @Override
|
25 |
| - public View getView(int position, View convertView, ViewGroup parent) { |
26 |
| - //User super class to create the View |
27 |
| - View v = super.getView(position, convertView, parent); |
28 |
| - assert v != null; |
29 |
| - TextView tv = (TextView) v.findViewById(android.R.id.text1); |
| 30 | + public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { |
| 31 | + return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(resource, viewGroup, false)); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void onBindViewHolder(ViewHolder holder, int position) { |
30 | 36 | //Put the image on the TextView
|
31 |
| - tv.setCompoundDrawablesWithIntrinsicBounds(items.get(position).icon, null, null, null); |
| 37 | + Item item = items.get(position); |
32 | 38 |
|
33 |
| - if (items.get(position).icon != null) { |
34 |
| - //Add margin between image and text (support various screen densities) |
35 |
| - int dp5 = (int) (5 * getContext().getResources().getDisplayMetrics().density + 0.5f); |
36 |
| - tv.setCompoundDrawablePadding(dp5); |
| 39 | + if (item.text != null) |
| 40 | + holder.tv.setText(item.text); |
| 41 | + |
| 42 | + holder.tv.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null); |
| 43 | + |
| 44 | + if (item.icon != null) { |
| 45 | + holder.tv.setCompoundDrawablePadding(dp5); |
37 | 46 | } else {
|
38 |
| - tv.setCompoundDrawablePadding(0); |
| 47 | + holder.tv.setCompoundDrawablePadding(0); |
39 | 48 | }
|
| 49 | + } |
40 | 50 |
|
41 |
| - return v; |
| 51 | + @Override |
| 52 | + public int getItemCount() { |
| 53 | + return items.size(); |
| 54 | + } |
| 55 | + |
| 56 | + public static class ViewHolder extends RecyclerView.ViewHolder { |
| 57 | + TextView tv; |
| 58 | + |
| 59 | + public ViewHolder(View itemView) { |
| 60 | + super(itemView); |
| 61 | + tv = (TextView) itemView.findViewById(textViewResourceId); |
| 62 | + } |
42 | 63 | }
|
43 | 64 |
|
44 | 65 | public static class Item {
|
|
0 commit comments