-
-
Save rohitnotes/6342d1cecdc58514cbc14e052470720c to your computer and use it in GitHub Desktop.
Databinding BaseAdapter based on george mount's medium post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.databinding.DataBindingUtil; | |
import android.databinding.ViewDataBinding; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* Created by Davud. MyApplication project. | |
*/ | |
public abstract class BaseAdapter<T> | |
extends RecyclerView.Adapter<BaseAdapter<T>.MyViewHolder> { | |
BaseAdapter(OnItemClickListener<T> itemClickListener) { | |
this.itemClickListener = itemClickListener; | |
} | |
public interface OnItemClickListener<T> { | |
void onItemClick(T item); | |
} | |
private final OnItemClickListener<T> itemClickListener; | |
public BaseAdapter<T>.MyViewHolder onCreateViewHolder(ViewGroup parent, | |
int viewType) { | |
LayoutInflater layoutInflater = | |
LayoutInflater.from(parent.getContext()); | |
ViewDataBinding binding = DataBindingUtil.inflate( | |
layoutInflater, viewType, parent, false); | |
return new BaseAdapter<T>.MyViewHolder(binding); | |
} | |
@Override | |
public void onBindViewHolder(BaseAdapter<T>.MyViewHolder holder, | |
int position) { | |
final T item = getItemForPosition(position); | |
holder.itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
itemClickListener.onItemClick(item); | |
} | |
}); | |
holder.bind(item); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return getLayoutIdForPosition(position); | |
} | |
protected abstract T getItemForPosition(int position); | |
protected abstract int getLayoutIdForPosition(int position); | |
public class MyViewHolder extends RecyclerView.ViewHolder { | |
private final ViewDataBinding binding; | |
public MyViewHolder(ViewDataBinding binding) { | |
super(binding.getRoot()); | |
this.binding = binding; | |
} | |
public void bind(T item) { | |
binding.setVariable(BR.obj, item); | |
binding.executePendingBindings(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
/** | |
* Created by Davud. MyApplication project. | |
*/ | |
public class MyAdapter extends BaseAdapter<ItemData> { | |
private final List<ItemData> data; | |
public MyAdapter(List<ItemData> data, OnItemClickListener<ItemData> itemClickListener) { | |
super(itemClickListener); | |
this.data = data; | |
} | |
@Override | |
protected ItemData getItemForPosition(int position) { | |
return data.get(position); | |
} | |
@Override | |
protected int getLayoutIdForPosition(int position) { | |
return R.layout.item_view; | |
} | |
@Override | |
public int getItemCount() { | |
return data.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment