|
| 1 | +package com.easywidgets.lists; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.MenuItem; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.ArrayAdapter; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.PopupMenu; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +import androidx.annotation.MenuRes; |
| 14 | +import androidx.annotation.NonNull; |
| 15 | +import androidx.annotation.Nullable; |
| 16 | + |
| 17 | +import com.easywidgets.R; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +/** |
| 22 | + * An extended version {@link ArrayAdapter} used for making custom listviews. You can make 2 types of listview using this adapter. |
| 23 | + * @see R.layout#list |
| 24 | + */ |
| 25 | +public class EasyListAdapter extends ArrayAdapter<String> { |
| 26 | + |
| 27 | + private final Context context; |
| 28 | + private String[] titles; |
| 29 | + private String[] metadata; |
| 30 | + private String[] description; |
| 31 | + private ArrayList<EasyListObject> objects; |
| 32 | + private int[] images; |
| 33 | + private int menu; |
| 34 | + private OnListMenuClickListener listener; |
| 35 | + private final int LIST; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates an adapter with 2 textview, one for heading & another for summery. |
| 39 | + * @param context The context of activity |
| 40 | + * @param titles The text to set as title |
| 41 | + * @param description The text to set as description |
| 42 | + */ |
| 43 | + public EasyListAdapter(@NonNull Context context, String[] titles, String[] description) { |
| 44 | + super(context, android.R.layout.simple_list_item_2); |
| 45 | + this.titles = titles; |
| 46 | + this.description = description; |
| 47 | + this.context = context; |
| 48 | + LIST = 0; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates an adapter with 3 textview & a imageview, 1st two for title & heading and another one for some meta data. You can pass |
| 53 | + * <code>null</code> if you don't want 3rd textview. |
| 54 | + * @param context The context of activity |
| 55 | + * @param images The image resource id to set as drawableStart or icon. |
| 56 | + * @param titles The text array to set as titles |
| 57 | + * @param description The text array to set as description |
| 58 | + * @param meta The text array to set as meta information |
| 59 | + */ |
| 60 | + public EasyListAdapter(@NonNull Context context, String[] titles, String[] description, int[] images, @Nullable String[] meta){ |
| 61 | + super(context,R.layout.list); |
| 62 | + this.titles = titles; |
| 63 | + this.metadata = meta; |
| 64 | + this.description = description; |
| 65 | + this.images = images; |
| 66 | + this.context = context; |
| 67 | + LIST = 1; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates an adapter through object. The {@link EasyListObject} constructors arguments determines the type of list |
| 72 | + * @param objects A {@link EasyListObject} POJO to set parameters of the listview |
| 73 | + */ |
| 74 | + public EasyListAdapter(@NonNull Context context, ArrayList<EasyListObject> objects){ |
| 75 | + super(context,R.layout.list); |
| 76 | + this.objects = objects; |
| 77 | + this.context = context; |
| 78 | + LIST = objects.get(0).getLIST(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates an adapter with 2 textview & a view at the right. The view can be button or an image view denoting menu options. |
| 83 | + * @param context The context of activity |
| 84 | + * @param menu The menu to show as more option of the item |
| 85 | + * @param titles The text array to set as titles |
| 86 | + * @param description The text array to set as headings |
| 87 | + * @param listener The callback on menu item click |
| 88 | + */ |
| 89 | + public EasyListAdapter(@NonNull Context context, String[] titles, String[] description, @MenuRes int menu, @NonNull OnListMenuClickListener listener){ |
| 90 | + super(context,R.layout.list); |
| 91 | + this.titles = titles; |
| 92 | + this.description = description; |
| 93 | + this.context = context; |
| 94 | + this.listener = listener; |
| 95 | + this.menu = menu; |
| 96 | + LIST = 2; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + @NonNull |
| 102 | + @Override |
| 103 | + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { |
| 104 | + View view; |
| 105 | + if (LIST == 0) { |
| 106 | + view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_2, parent, false); |
| 107 | + TextView title = view.findViewById(android.R.id.text1); |
| 108 | + TextView heading = view.findViewById(android.R.id.text2); |
| 109 | + |
| 110 | + title.setText(titles[position]); |
| 111 | + heading.setText(description[position]); |
| 112 | + } else if (LIST == 1){ |
| 113 | + view = LayoutInflater.from(context).inflate(R.layout.list,parent,false); |
| 114 | + TextView title = view.findViewById(R.id.title); |
| 115 | + TextView heading = view.findViewById(R.id.heading); |
| 116 | + TextView summary = view.findViewById(R.id.summery); |
| 117 | + ImageView icon = view.findViewById(R.id.icon); |
| 118 | + |
| 119 | + if (objects == null){ |
| 120 | + title.setText(titles[position]); |
| 121 | + heading.setText(description[position]); |
| 122 | + summary.setText(metadata != null ? metadata[position] : ""); |
| 123 | + icon.setImageResource(images[position]); |
| 124 | + } else { |
| 125 | + EasyListObject object = objects.get(position); |
| 126 | + title.setText(object.getTitle()); |
| 127 | + heading.setText(object.getHeading()); |
| 128 | + summary.setText(object.getSummary() != null ? object.getSummary() : ""); |
| 129 | + icon.setImageResource(object.getIcon()); |
| 130 | + } |
| 131 | + } else if (LIST == 2){ |
| 132 | + view = LayoutInflater.from(context).inflate(R.layout.list_meta,parent, false); |
| 133 | + TextView title = view.findViewById(R.id.title); |
| 134 | + TextView heading = view.findViewById(R.id.heading); |
| 135 | + ImageView more = view.findViewById(R.id.menu); |
| 136 | + PopupMenu menu = new PopupMenu(context,more); |
| 137 | + menu.inflate(this.menu); |
| 138 | + menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { |
| 139 | + @Override |
| 140 | + public boolean onMenuItemClick(MenuItem item) { |
| 141 | + listener.onMenuItemClick(item.getItemId(),position); |
| 142 | + return true; |
| 143 | + } |
| 144 | + }); |
| 145 | + more.setOnClickListener(new View.OnClickListener() { |
| 146 | + @Override |
| 147 | + public void onClick(View v) { |
| 148 | + menu.show(); |
| 149 | + } |
| 150 | + }); |
| 151 | + if (objects == null){ |
| 152 | + title.setText(titles[position]); |
| 153 | + heading.setText(description[position]); |
| 154 | + } else { |
| 155 | + EasyListObject object = objects.get(position); |
| 156 | + title.setText(object.getTitle()); |
| 157 | + heading.setText(object.getHeading()); |
| 158 | + } |
| 159 | + } else { |
| 160 | + throw new IllegalStateException("Impossible without using reflection !"); |
| 161 | + } |
| 162 | + return view; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public int getCount() { |
| 167 | + return objects != null ? objects.size() : titles.length; |
| 168 | + } |
| 169 | +} |
0 commit comments