Skip to content

Instantly share code, notes, and snippets.

@mannodermaus
Last active June 14, 2023 14:24
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mannodermaus/ebf8ec212e4296aebb24 to your computer and use it in GitHub Desktop.
Save mannodermaus/ebf8ec212e4296aebb24 to your computer and use it in GitHub Desktop.
RecyclerView.ViewHolder and Adapter demonstration
public abstract class BaseAdapter<T, VH extends BaseViewHolder<T>> extends RecyclerView.Adapter<VH> {
private List<T> items;
// Missing: setItems(), addItem(), removeItem(), ...
@Override
public final void onBindViewHolder(VH vh, int position) {
T item = items.get(position);
vh.performBind(item, position);
}
}
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private T item;
protected BaseViewHolder(ViewGroup parent, @LayoutRes int itemLayoutId) {
super(LayoutInflater.from(parent.getContext()).inflate(itemLayoutId, parent, false));
// Bind instance views and attach listeners to the itemView (this field is provided by RecyclerView.ViewHolder itself)
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
protected final void performBind(T item, int position) {
this.item = item;
onBind(item, position);
}
protected T getItem() {
return item;
}
protected abstract void onBind(T item, int position);
protected abstract void onClick(View view, T item);
protected abstract void onLongClick(View view, T item);
@Override
public final void onClick(View view) {
onClick(view, item);
}
@Override public final boolean onLongClick(View view) {
return onLongClick(view, item);
}
/**
* Just some demo POJO to populate a list with.
*/
public class Demo {
public final String name;
public final String url;
public final float value;
public Demo(String name, String url, float value) {
this.name = name;
this.url = url;
this.value = value;
}
}
public class DemoAdapter extends BaseAdapter<Demo, DemoViewHolder> {
@Override
public final DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new DemoViewHolder(parent);
}
}
public class DemoViewHolder extends BaseViewHolder<Demo> {
@Bind(R.id.textview_name)
TextView tvName;
@Bind(R.id.imageview_url)
ImageView ivUrl;
@Bind(R.id.imageview_context_menu)
ImageView ivContextMenu;
public DemoViewHolder(ViewGroup parent) {
// Call through to super constructor; ButterKnife will have bound instance fields after this
super(parent, R.layout.item_demo);
// Without ButterKnife, this would be where you findViewById on the itemView,
// e.g. tvName = (TextView) itemView.findViewById(R.id.textview_name);
// Now, add more listeners, if needed
ivContextMenu.setOnClickListener((view) -> {
// Do something specific when clicking this ImageView specifically.
// If you need to grab the ViewHolder's current item,
// use the base class method getItem().
});
}
@Override
public void onBind(Demo item, int position) {
tvName.setText(item.name);
Glide.with(ivUrl.getContext()).load(item.url).into(ivUrl);
}
@Override
public void onClick(View view, Demo item) {
// Do something on click!
// Maybe use a Bus mechanism (connected to the outer Activity/Fragment) to propagate the event?
// This could be passed in from the adapter in the constructor, for instance.
// bus.post(new OnDemoClicked(item));
}
@Override
public void onLongClick(View view, Demo item) {
// If you want additional behaviour upon long-clicking the item
}
}
@rohanagarwal94
Copy link

hi,
could you please be more elaborate on the bus mechanism. What i am trying to do is, on click of a button attached to each recyclerview item , I want to open a dialog which includes another recyclerview much like the Facebook UI. But I am unable to set the adapter despite the code of the dialog recyclerview being roughly same as that of my activty recyclerview. Would be grateful if you could help. Thanks!

@nieldeokar
Copy link

protected abstract void onLongClick(View view, T item);

this should be

protected abstract boolean onLongClick(View view, T item);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment