ListItemAdapter: use ViewHolder

This commit is contained in:
Daniel Gultsch 2017-11-23 23:26:19 +01:00
parent d95ff134d9
commit 7f4f30c849
1 changed files with 122 additions and 100 deletions

View File

@ -25,12 +25,14 @@ 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) {
@ -41,111 +43,12 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
}
}
};
private OnTagClickedListener mOnTagClickedListener = null;
public ListItemAdapter(XmppActivity activity, List<ListItem> objects) {
super(activity, 0, objects);
this.activity = activity;
}
public void refreshSettings() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
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);
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);
List<ListItem.Tag> tags = item.getTags(activity);
if (tags.size() == 0 || !this.showDynamicTags) {
tagLayout.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);
tv.setText(tag.getName());
tv.setBackgroundColor(tag.getColor());
tv.setOnClickListener(this.onTagTvClick);
tagLayout.addView(tv);
}
}
final String jid = item.getDisplayJid();
if (jid != null) {
tvJid.setVisibility(View.VISIBLE);
tvJid.setText(jid);
} else {
tvJid.setVisibility(View.GONE);
}
tvName.setText(item.getDisplayName());
loadAvatar(item,picture);
return view;
}
public void setOnTagClickedListener(OnTagClickedListener listener) {
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);
if (bm != null) {
cancelPotentialWork(item, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(item.getDisplayName()));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(item);
} catch (final RejectedExecutionException ignored) {
}
}
}
}
public static boolean cancelPotentialWork(ListItem item, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
@ -171,6 +74,100 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
return null;
}
public void refreshSettings() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
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);
ListItem item = getItem(position);
if (view == null) {
view = inflater.inflate(R.layout.contact, parent, false);
}
ViewHolder viewHolder = ViewHolder.get(view);
List<ListItem.Tag> tags = item.getTags(activity);
if (tags.size() == 0 || !this.showDynamicTags) {
viewHolder.tags.setVisibility(View.GONE);
} else {
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);
viewHolder.tags.addView(tv);
}
}
final String jid = item.getDisplayJid();
if (jid != null) {
viewHolder.jid.setVisibility(View.VISIBLE);
viewHolder.jid.setText(jid);
} else {
viewHolder.jid.setVisibility(View.GONE);
}
viewHolder.name.setText(EmojiWrapper.transform(item.getDisplayName()));
loadAvatar(item, viewHolder.avatar);
return view;
}
public void setOnTagClickedListener(OnTagClickedListener listener) {
this.mOnTagClickedListener = listener;
}
public void loadAvatar(ListItem item, ImageView imageView) {
if (cancelPotentialWork(item, imageView)) {
final Bitmap bm = activity.avatarService().get(item, activity.getPixel(48), true);
if (bm != null) {
cancelPotentialWork(item, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(item.getDisplayName()));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(item);
} catch (final RejectedExecutionException ignored) {
}
}
}
}
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() {
}
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;
}
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
@ -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);
}
}
}
}
}