use data binder for list item adapater

This commit is contained in:
Daniel Gultsch 2018-02-19 13:12:45 +01:00
parent 595757fe00
commit fc5a2922e9

View file

@ -3,6 +3,7 @@ package eu.siacs.conversations.ui.adapter;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -22,6 +23,7 @@ import java.util.List;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ContactBinding;
import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.SettingsActivity;
import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.XmppActivity;
@ -81,14 +83,17 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
@Override @Override
public View getView(int position, View view, ViewGroup parent) { public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = activity.getLayoutInflater();
ListItem item = getItem(position); ListItem item = getItem(position);
ViewHolder viewHolder;
if (view == null) { if (view == null) {
view = inflater.inflate(R.layout.contact, parent, false); ContactBinding binding = DataBindingUtil.inflate(inflater,R.layout.contact,parent,false);
viewHolder = ViewHolder.get(binding);
view = binding.getRoot();
} else {
viewHolder = (ViewHolder) view.getTag();
} }
ViewHolder viewHolder = ViewHolder.get(view);
List<ListItem.Tag> tags = item.getTags(activity); List<ListItem.Tag> tags = item.getTags(activity);
if (tags.size() == 0 || !this.showDynamicTags) { if (tags.size() == 0 || !this.showDynamicTags) {
viewHolder.tags.setVisibility(View.GONE); viewHolder.tags.setVisibility(View.GONE);
@ -155,16 +160,13 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
} }
public static ViewHolder get(View layout) { public static ViewHolder get(ContactBinding binding) {
ViewHolder viewHolder = (ViewHolder) layout.getTag(); ViewHolder viewHolder = new ViewHolder();
if (viewHolder == null) { viewHolder.name = binding.contactDisplayName;
viewHolder = new ViewHolder(); viewHolder.jid = binding.contactJid;
viewHolder.name = layout.findViewById(R.id.contact_display_name); viewHolder.avatar = binding.contactPhoto;
viewHolder.jid = layout.findViewById(R.id.contact_jid); viewHolder.tags = binding.tags;
viewHolder.avatar = layout.findViewById(R.id.contact_photo); binding.getRoot().setTag(viewHolder);
viewHolder.tags = layout.findViewById(R.id.tags);
layout.setTag(viewHolder);
}
return viewHolder; return viewHolder;
} }
} }
@ -192,7 +194,8 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
@Override @Override
protected Bitmap doInBackground(ListItem... params) { protected Bitmap doInBackground(ListItem... params) {
return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled()); this.item = params[0];
return activity.avatarService().get(this.item, activity.getPixel(48), isCancelled());
} }
@Override @Override