Created
February 16, 2018 08:12
-
-
Save DHosseiny/ee51978611c431e0afab7cf531deca08 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
Thanks for the code.
can you help me out with this...
what is BR.obj at line no. 63 in BaseAdapter.java
Thanks in advance