Skip to content

Instantly share code, notes, and snippets.

@Guneetgstar
Last active May 7, 2020 06:41
Show Gist options
  • Save Guneetgstar/bb5fce2b5e810ae7f7111b78335b04cb to your computer and use it in GitHub Desktop.
Save Guneetgstar/bb5fce2b5e810ae7f7111b78335b04cb to your computer and use it in GitHub Desktop.
A Simple Generic Implementation of `RecyclerView.Adapter`.
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.core.util.Supplier;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SimpleRecyclerViewAdapter<T> extends RecyclerView.Adapter<SimpleRecyclerViewAdapter.ViewHolder> {
private final int resource;
private int itemCount = 0;
private List<T> items;
private OnBindViewHolderListener onBindViewHolderListener;
private OnCreateViewHolderListener onCreateViewHolderListener;
public SimpleRecyclerViewAdapter(@LayoutRes final int resource) {
this(resource, new ArrayList<>(0));
}
public SimpleRecyclerViewAdapter(@LayoutRes final int resource, List<T> items) {
this.resource = resource;
this.setItems(items);
}
@NonNull
@Override
public SimpleRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View layoutView = LayoutInflater.from(viewGroup.getContext()).inflate(resource, viewGroup, false);
return this.onCreateViewHolderListener == null ? new SimpleRecyclerViewAdapter.ViewHolder(layoutView) : new SimpleRecyclerViewAdapter.ViewHolder(layoutView, onCreateViewHolderListener);
}
@Override
public void onBindViewHolder(@NonNull SimpleRecyclerViewAdapter.ViewHolder viewHolder, int i) {
if (onBindViewHolderListener != null) {
onBindViewHolderListener.onBind(viewHolder, i);
}
}
public void setItemCount(int count){
this.itemCount=count;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (itemCount > items.size())
return itemCount;
return items.size();
}
public void setItem(int pos, T item) {
this.getItems().set(pos, item);
notifyItemChanged(pos);
}
public void setItems(List<T> items) {
this.items = items;
notifyDataSetChanged();
}
public void setItems(T[] items) {
this.items = Arrays.asList(items);
notifyDataSetChanged();
}
public void addItem(int pos, T item) {
this.items.add(pos, item);
notifyItemInserted(pos);
}
public void removeItem(int pos) {
this.items.remove(pos);
notifyItemRemoved(pos);
}
public List<T> getItems() {
return this.items;
}
public SimpleRecyclerViewAdapter<T> setOnBindViewHolderListener(OnBindViewHolderListener onBindViewHolderListener) {
this.onBindViewHolderListener = onBindViewHolderListener;
return this;
}
public OnCreateViewHolderListener getOnCreateViewHolderListener() {
return onCreateViewHolderListener;
}
public void setOnCreateViewHolderListener(OnCreateViewHolderListener onCreateViewHolderListener) {
this.onCreateViewHolderListener = onCreateViewHolderListener;
}
public OnBindViewHolderListener getOnBindViewHolderListener() {
return onBindViewHolderListener;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(@NonNull View itemView) {
super(itemView);
}
ViewHolder(@NonNull View itemView, OnCreateViewHolderListener listener) {
super(itemView);
listener.onCreate(this, this::getAdapterPosition);
}
}
public interface OnCreateViewHolderListener {
void onCreate(SimpleRecyclerViewAdapter.ViewHolder viewHolder, Supplier<Integer> supplier);
}
public interface OnBindViewHolderListener {
void onBind(SimpleRecyclerViewAdapter.ViewHolder viewHolder, int pos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment