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(); | |
} | |
} |
It's very good. tnx
I wrote Kotlin codes for this, inspired by your code.
See the link below
https://gist.github.com/DHosseiny/ee51978611c431e0afab7cf531deca08
@alzahm Ok it's better.
@hosseiniSeyRo Thanks.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your thought but I don't think your solution is very good for two reasons. First, you are creating a new click listener every time onBindViewHolder gets called and that's not good for performance. You should not instantiate objects in onBindViewHolder, that's against Android best practices. Secondly one of the benefits of recyclerview over listview is the ability to set different click listener for different views in the layout and you're just ignoring this. Even though your code may work most of the time it's not a generic solution so not all Adapters can extend from this.
I posted my solution here. I tried to be as generic as possible so it can work with handling any events even custom ones. you can check it out. I would be happy to know your ideas about this. thanks
https://medium.com/@a.ahmadi.dev_34272/thank-you-for-your-great-post-i-learned-a-lot-e24de2371166