aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel@gultsch.de>2017-11-23 23:26:19 +0100
committerDaniel Gultsch <daniel@gultsch.de>2017-11-23 23:26:19 +0100
commit7f4f30c84901655c9312b9aa2abe0e4ebca75029 (patch)
tree580476b4963c58b364e3899ecd402e0060b09708
parentd95ff134d9e93c73193dc9f0fb01b9feec43fe47 (diff)
ListItemAdapter: use ViewHolder
-rw-r--r--src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java158
1 files changed, 90 insertions, 68 deletions
diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java
index 11027d8..65fafdb 100644
--- a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java
+++ b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java
@@ -25,70 +25,93 @@ import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.ui.SettingsActivity;
import eu.siacs.conversations.ui.XmppActivity;
+import eu.siacs.conversations.utils.EmojiWrapper;
import eu.siacs.conversations.utils.UIHelper;
public class ListItemAdapter extends ArrayAdapter<ListItem> {
protected XmppActivity activity;
protected boolean showDynamicTags = false;
+ private OnTagClickedListener mOnTagClickedListener = null;
private View.OnClickListener onTagTvClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
- if (view instanceof TextView && mOnTagClickedListener != null) {
+ if (view instanceof TextView && mOnTagClickedListener != null) {
TextView tv = (TextView) view;
final String tag = tv.getText().toString();
mOnTagClickedListener.onTagClicked(tag);
}
}
};
- private OnTagClickedListener mOnTagClickedListener = null;
public ListItemAdapter(XmppActivity activity, List<ListItem> objects) {
super(activity, 0, objects);
this.activity = activity;
}
+ public static boolean cancelPotentialWork(ListItem item, ImageView imageView) {
+ final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
+
+ if (bitmapWorkerTask != null) {
+ final ListItem oldItem = bitmapWorkerTask.item;
+ if (oldItem == null || item != oldItem) {
+ bitmapWorkerTask.cancel(true);
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
+ if (imageView != null) {
+ final Drawable drawable = imageView.getDrawable();
+ if (drawable instanceof AsyncDrawable) {
+ final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
+ return asyncDrawable.getBitmapWorkerTask();
+ }
+ }
+ return null;
+ }
+
public void refreshSettings() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
- this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS,false);
+ this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, false);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) getContext()
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListItem item = getItem(position);
if (view == null) {
view = inflater.inflate(R.layout.contact, parent, false);
}
- TextView tvName = (TextView) view.findViewById(R.id.contact_display_name);
- TextView tvJid = (TextView) view.findViewById(R.id.contact_jid);
- ImageView picture = (ImageView) view.findViewById(R.id.contact_photo);
- FlowLayout tagLayout = (FlowLayout) view.findViewById(R.id.tags);
+
+ ViewHolder viewHolder = ViewHolder.get(view);
List<ListItem.Tag> tags = item.getTags(activity);
if (tags.size() == 0 || !this.showDynamicTags) {
- tagLayout.setVisibility(View.GONE);
+ viewHolder.tags.setVisibility(View.GONE);
} else {
- tagLayout.setVisibility(View.VISIBLE);
- tagLayout.removeAllViewsInLayout();
- for(ListItem.Tag tag : tags) {
- TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tagLayout,false);
+ viewHolder.tags.setVisibility(View.VISIBLE);
+ viewHolder.tags.removeAllViewsInLayout();
+ for (ListItem.Tag tag : tags) {
+ TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag, viewHolder.tags, false);
tv.setText(tag.getName());
tv.setBackgroundColor(tag.getColor());
tv.setOnClickListener(this.onTagTvClick);
- tagLayout.addView(tv);
+ viewHolder.tags.addView(tv);
}
}
final String jid = item.getDisplayJid();
if (jid != null) {
- tvJid.setVisibility(View.VISIBLE);
- tvJid.setText(jid);
+ viewHolder.jid.setVisibility(View.VISIBLE);
+ viewHolder.jid.setText(jid);
} else {
- tvJid.setVisibility(View.GONE);
+ viewHolder.jid.setVisibility(View.GONE);
}
- tvName.setText(item.getDisplayName());
- loadAvatar(item,picture);
+ viewHolder.name.setText(EmojiWrapper.transform(item.getDisplayName()));
+ loadAvatar(item, viewHolder.avatar);
return view;
}
@@ -96,38 +119,9 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
this.mOnTagClickedListener = listener;
}
- public interface OnTagClickedListener {
- void onTagClicked(String tag);
- }
-
- class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> {
- private final WeakReference<ImageView> imageViewReference;
- private ListItem item = null;
-
- public BitmapWorkerTask(ImageView imageView) {
- imageViewReference = new WeakReference<>(imageView);
- }
-
- @Override
- protected Bitmap doInBackground(ListItem... params) {
- return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
- }
-
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- if (bitmap != null && !isCancelled()) {
- final ImageView imageView = imageViewReference.get();
- if (imageView != null) {
- imageView.setImageBitmap(bitmap);
- imageView.setBackgroundColor(0x00000000);
- }
- }
- }
- }
-
public void loadAvatar(ListItem item, ImageView imageView) {
if (cancelPotentialWork(item, imageView)) {
- final Bitmap bm = activity.avatarService().get(item,activity.getPixel(48),true);
+ final Bitmap bm = activity.avatarService().get(item, activity.getPixel(48), true);
if (bm != null) {
cancelPotentialWork(item, imageView);
imageView.setImageBitmap(bm);
@@ -146,29 +140,32 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
}
}
- public static boolean cancelPotentialWork(ListItem item, ImageView imageView) {
- final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
+ public interface OnTagClickedListener {
+ void onTagClicked(String tag);
+ }
+
+ private static class ViewHolder {
+ private TextView name;
+ private TextView jid;
+ private ImageView avatar;
+ private FlowLayout tags;
+
+ private ViewHolder() {
- if (bitmapWorkerTask != null) {
- final ListItem oldItem = bitmapWorkerTask.item;
- if (oldItem == null || item != oldItem) {
- bitmapWorkerTask.cancel(true);
- } else {
- return false;
- }
}
- return true;
- }
- private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
- if (imageView != null) {
- final Drawable drawable = imageView.getDrawable();
- if (drawable instanceof AsyncDrawable) {
- final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
- return asyncDrawable.getBitmapWorkerTask();
+ public static ViewHolder get(View layout) {
+ ViewHolder viewHolder = (ViewHolder) layout.getTag();
+ if (viewHolder == null) {
+ viewHolder = new ViewHolder();
+ viewHolder.name = layout.findViewById(R.id.contact_display_name);
+ viewHolder.jid = layout.findViewById(R.id.contact_jid);
+ viewHolder.avatar = layout.findViewById(R.id.contact_photo);
+ viewHolder.tags = layout.findViewById(R.id.tags);
+ layout.setTag(viewHolder);
}
+ return viewHolder;
}
- return null;
}
static class AsyncDrawable extends BitmapDrawable {
@@ -184,4 +181,29 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
}
}
+ class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> {
+ private final WeakReference<ImageView> imageViewReference;
+ private ListItem item = null;
+
+ public BitmapWorkerTask(ImageView imageView) {
+ imageViewReference = new WeakReference<>(imageView);
+ }
+
+ @Override
+ protected Bitmap doInBackground(ListItem... params) {
+ return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
+ }
+
+ @Override
+ protected void onPostExecute(Bitmap bitmap) {
+ if (bitmap != null && !isCancelled()) {
+ final ImageView imageView = imageViewReference.get();
+ if (imageView != null) {
+ imageView.setImageBitmap(bitmap);
+ imageView.setBackgroundColor(0x00000000);
+ }
+ }
+ }
+ }
+
}