Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit d3c650d

Browse files
authored
version 2
1 parent 27f1446 commit d3c650d

31 files changed

+1069
-0
lines changed

EasyWidgets/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
id 'com.android.library'
3+
}
4+
5+
android {
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.3"
8+
9+
defaultConfig {
10+
minSdkVersion 23
11+
targetSdkVersion 30
12+
versionCode 2
13+
versionName "2.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
consumerProguardFiles "consumer-rules.pro"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
implementation 'androidx.appcompat:appcompat:1.3.0'
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.easywidgets">
4+
5+
</manifest>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.easywidgets.lists;
2+
3+
import android.widget.Button;
4+
5+
import androidx.annotation.MenuRes;
6+
import androidx.annotation.NonNull;
7+
import androidx.annotation.Nullable;
8+
9+
public class EasyListObject {
10+
private String title;
11+
private String heading;
12+
private String summary;
13+
private int menu;
14+
private OnListMenuClickListener listener;
15+
private int icon;
16+
private int LIST;
17+
18+
19+
public EasyListObject(int icon, @NonNull String title, @NonNull String heading, @Nullable String summary){
20+
this.title = title;
21+
this.heading = heading;
22+
this.summary = summary;
23+
this.icon = icon;
24+
LIST = 1;
25+
}
26+
27+
28+
public EasyListObject(@NonNull String title, @NonNull String heading,@MenuRes int menuId, @Nullable OnListMenuClickListener listener){
29+
this.title = title;
30+
this.heading = heading;
31+
this.menu = menuId;
32+
this.listener = listener;
33+
LIST = 2;
34+
}
35+
36+
37+
public EasyListObject(){} // Requires an empty constructor, in case if you are using firebase database.
38+
39+
public String getTitle() {
40+
return title;
41+
}
42+
43+
public void setTitle(String title) {
44+
this.title = title;
45+
}
46+
47+
public String getHeading() {
48+
return heading;
49+
}
50+
51+
public void setHeading(String heading) {
52+
this.heading = heading;
53+
}
54+
55+
public @Nullable String getSummary() {
56+
return summary;
57+
}
58+
59+
public void setSummary(String summary) {
60+
this.summary = summary;
61+
}
62+
63+
public int getIcon() {
64+
return icon;
65+
}
66+
67+
public void setIcon(int icon) {
68+
this.icon = icon;
69+
}
70+
71+
public int getMenu() {
72+
return menu;
73+
}
74+
75+
public void setMenu(int menu) {
76+
this.menu = menu;
77+
}
78+
79+
public OnListMenuClickListener getListener() {
80+
return listener;
81+
}
82+
83+
public void setListener(OnListMenuClickListener listener) {
84+
this.listener = listener;
85+
}
86+
87+
public int getLIST() {
88+
return LIST;
89+
}
90+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.easywidgets.lists;
2+
3+
public interface OnListMenuClickListener {
4+
void onMenuItemClick(int itemId,int listPosition);
5+
}

0 commit comments

Comments
 (0)