How can I use Android DataBinding in a listview and still use a ViewHolder pattern?
Try this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = ((Activity) parent.getContext()).getLayoutInflater();
}
// Perform the binding
ActivityTeamMessageListRowBinding binding = DataBindingUtil.getBinding(convertView);
if (binding == null) {
binding = DataBindingUtil.inflate(inflater, R.layout.my_activity_list_row, parent, false);
}
binding.setInfo(list.get(position));
binding.executePendingBindings();
// Return the bound view
return binding.getRoot();
}
I haven't used data binding with ListView
(I'll use RecyclerView
), but off the cuff, this is what I'd try. Use breakpoints or logging to confirm that, when convertView
is not null
, that you get binding
back from getBinding()
more often than not (and perhaps all the time — I'm hazy on how data binding's caching works).