-
-
Save idotnetdev/158521f19b56bde85814556e24cc3984 to your computer and use it in GitHub Desktop.
Abstract RecyclerView.Adapter
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
//T is a type of items | |
//when inheriting, just create constructor matching super, | |
//and override viewHolder() method that will return ViewHolder for given item type | |
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> { | |
public interface OnItemClickListener<T> { | |
void onClick(T item); | |
} | |
public interface OnItemLongClickListener<T> { | |
void onLongClick(T item); | |
} | |
private OnItemClickListener<T> onItemClickListener; | |
private OnItemLongClickListener<T> onItemLongClickListener; | |
private ArrayList<T> items; | |
private Context context; | |
private RecyclerView.LayoutManager layoutManager; | |
@LayoutRes | |
private int holderLayout; | |
public BaseRecyclerAdapter() { | |
this(new ArrayList<T>()); | |
} | |
public BaseRecyclerAdapter(@NonNull T[] items) { | |
this(new ArrayList<>(Arrays.asList(items))); | |
} | |
public BaseRecyclerAdapter(@NonNull ArrayList<T> items) { | |
this.items = items; | |
if (getClass().isAnnotationPresent(Layout.class)) | |
holderLayout = getClass().getAnnotation(Layout.class).value(); | |
} | |
@Override | |
public void onAttachedToRecyclerView(RecyclerView recyclerView) { | |
super.onAttachedToRecyclerView(recyclerView); | |
context = recyclerView.getContext(); | |
layoutManager = recyclerView.getLayoutManager(); | |
} | |
@Override | |
public void onDetachedFromRecyclerView(RecyclerView recyclerView) { | |
super.onDetachedFromRecyclerView(recyclerView); | |
context = null; | |
layoutManager = null; | |
} | |
@Override | |
public BaseViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) { | |
return viewHolder( | |
LayoutInflater.from(parent.getContext()).inflate(holderLayout, parent, false) | |
); | |
} | |
@Override | |
public void onBindViewHolder(BaseViewHolder holder, int position) { | |
holder.bind(items.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
return items.size(); | |
} | |
public ArrayList<T> getItems() { | |
return items; | |
} | |
public Context getContext() { | |
return context; | |
} | |
public RecyclerView.LayoutManager getLayoutManager() { | |
return layoutManager; | |
} | |
public void addAll(ArrayList<T> newItems) { | |
int positionStart = items.size(); | |
items.addAll(newItems); | |
notifyItemRangeInserted(positionStart, items.size() - 1); | |
} | |
public void add(T item) { | |
items.add(item); | |
notifyItemInserted(items.size() - 1); | |
} | |
public void add(T item, int position) { | |
items.add(position, item); | |
notifyItemInserted(position); | |
} | |
public void remove(T item) { | |
int index = items.indexOf(item); | |
items.remove(item); | |
notifyItemRemoved(index); | |
} | |
public void remove(int position) { | |
items.remove(position); | |
notifyItemRemoved(position); | |
} | |
public void replaceItems(@NonNull ArrayList<T> newItems) { | |
items = newItems; | |
notifyDataSetChanged(); | |
} | |
public void setOnItemClickListener(@Nullable OnItemClickListener<T> onItemClickListener) { | |
this.onItemClickListener = onItemClickListener; | |
} | |
public void setOnItemLongClickListener(@Nullable OnItemLongClickListener<T> onItemLongClickListener) { | |
this.onItemLongClickListener = onItemLongClickListener; | |
} | |
protected abstract BaseViewHolder<T> viewHolder(View itemView); | |
public abstract class BaseViewHolder<IT> extends RecyclerView.ViewHolder | |
implements View.OnClickListener, View.OnLongClickListener { | |
public BaseViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
itemView.setOnClickListener(this); | |
itemView.setOnLongClickListener(this); | |
} | |
protected abstract void bind(IT item); | |
@Override | |
public void onClick(View view) { | |
if (onItemClickListener != null) | |
onItemClickListener.onClick(items.get(getAdapterPosition())); | |
} | |
@Override | |
public boolean onLongClick(View view) { | |
if (onItemLongClickListener != null) { | |
onItemLongClickListener.onLongClick(items.get(getAdapterPosition())); | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
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
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.TYPE) | |
public @interface Layout { | |
@LayoutRes int value(); | |
} |
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
//Example of BaseRecyclerAdapter implementation | |
//Annotate your adapters with Layout annotation (with layout id of your list items) | |
@Layout(R.layout.text_item) | |
public class StringAdapter extends BaseRecyclerAdapter<String> { | |
public StringAdapter(@NonNull String[] items) { | |
super(items); | |
} | |
@Override | |
protected BaseViewHolder<String> viewHolder(View itemView) { | |
return new ViewHolder(itemView); | |
} | |
public class ViewHolder extends BaseViewHolder<String> { | |
@Nullable | |
@BindView(R.id.tvText) | |
TextView tvText; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
} | |
public void bind(String item) { | |
tvText.setText(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment