Skip to content

Instantly share code, notes, and snippets.

@IshankGulati
Last active December 2, 2020 07:54
Show Gist options
  • Save IshankGulati/1e1390ad8a1bace27ea6900dbd35467c to your computer and use it in GitHub Desktop.
Save IshankGulati/1e1390ad8a1bace27ea6900dbd35467c to your computer and use it in GitHub Desktop.
Base Adapter and RecyclerView.ViewHolder implementation for delegating clicks to Fragment to which adapter is attached. This gist is inspired from https://gist.github.com/aurae/ebf8ec212e4296aebb24 .
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Ishank Gulati on 14/10/16.
* Base adapter to be extended by all the recycler view adapters.
*/
public abstract class RecyclerViewBaseAdapter<T, VH extends RecyclerViewBaseViewHolder> extends
RecyclerView.Adapter<VH> implements RVObservable{
protected List<T> items;
private RecyclerViewItemClickListener listener;
private RVObserver observer;
@Override
public final VH onCreateViewHolder(ViewGroup parent, int viewType) {
VH vh = onCreateRecyclerViewHolder(parent, viewType);
notifyListenerAttached();
return vh;
}
@Override
public final void onBindViewHolder(VH holder, int position) {
if (items != null) {
holder.performBind(items.get(position), position);
}
onBindRecyclerViewHolder(holder, position);
}
/**
* Functionality of onCreateViewHolder has been moved here. User should override this when using
* this class
* @param parent The ViewGroup into which the new View will be added after it is bound to
* an adapter position.
* @param viewType The view type of the new View.
* @return A new ViewHolder that holds a View of the given view type.
*/
public abstract VH onCreateRecyclerViewHolder(ViewGroup parent, int viewType);
/**
* Functionality of onBindViewHolder has been moved here. User should override this when using
* this class
* @param holder The ViewHolder which should be updated to represent the contents of the
* item at the given position in the data set.
* @param position The position of the item within the adapter's data set.
*/
public abstract void onBindRecyclerViewHolder(VH holder, int position);
/**
* Method used to set on item click listener of Recycler view.
* @param listener An implementation of RecyclerViewItemClickListener
*/
public void setOnItemClickListener(RecyclerViewItemClickListener listener){
this.listener = listener;
notifyListenerAttached();
}
/**
* Used to update adapter's data
* @param items adapter's data
*/
public void setDataset(List<T> items) {
this.items = items;
notifyDataSetChanged();
}
/**
* Used to register view holder in adapter.
* @param o view holder implementing observer interface.
*/
@Override
public final void registerObserver(RVObserver o) {
this.observer = o;
}
/**
* Used to remove registered view holder in adapter.
*/
@Override
public final void removeObserver() {
this.observer = null;
}
/**
* Used to notify view holder that on click listener of recycler view has been updated.
*/
@Override
public final void notifyListenerAttached(){
if (observer != null && listener != null) {
observer.update(listener);
}
}
}
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by Ishank Gulati on 14/10/16.
* Base view holder to be extended by all the recycler view view holders.
*/
public abstract class RecyclerViewBaseViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnLongClickListener, RVObserver{
protected T item;
protected int position;
private RecyclerViewItemClickListener listener;
private RVObservable observable;
protected RecyclerViewBaseViewHolder(View itemView, RVObservable observable) {
super(itemView);
this.observable = observable;
observable.registerObserver(this);
}
/**
* Used to bind items to view holder.
* @param item item within the adapter's data set
* @param position The position of the item within the adapter's data set
*/
protected final void performBind(T item, int position) {
this.item = item;
this.position = position;
}
/**
* onClick has been delegated to fragment having recycler view.
* @param view view which is clicked
*/
@Override
public final void onClick(View view) {
if(listener != null) {
listener.onClick(view, item);
}
}
/**
* onLongClick has been delegated to fragment having recycler view.
* @param view view which is clicked
*/
@Override
public final boolean onLongClick(View view) {
return listener != null && listener.onLongClick(view, item);
}
/**
* Used to update view holder with a listener listening to all the clicks on recycler view.
* @param listener An implementation of RecyclerViewItemClickListener
*/
@Override
public final void update(RecyclerViewItemClickListener listener){
this.listener = listener;
}
}
import android.view.View;
/**
* Created by Ishank Gulati on 14/10/16.
*/
public interface RecyclerViewItemClickListener<T> {
void onClick(View view, T item);
boolean onLongClick(View view, T item);
}
/**
* Created by Ishank Gulati on 14/10/16.
* Subject as per Observer design pattern.
*/
public interface RVObservable {
void registerObserver(RVObserver o);
void notifyListenerAttached();
void removeObserver();
}
/**
* Created by Ishank Gulati on 14/10/16.
* Observer as per Observer design pattern.
*/
public interface RVObserver {
void update(RecyclerViewItemClickListener listener);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment