1 package com.moiseum.wolnelektury.components.recycler;
3 import android.content.Context;
4 import android.support.v7.widget.RecyclerView;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.view.ViewGroup;
9 import com.moiseum.wolnelektury.connection.models.CategoryModel;
11 import java.util.Collections;
12 import java.util.List;
18 public abstract class RecyclerAdapter<T, VH extends ViewHolder> extends RecyclerView.Adapter<VH> {
23 public interface OnItemClickListener<T> {
25 * @param item clicked item
26 * @param view clicked view
28 void onItemClicked(T item, View view, int position);
31 public enum Selection {
35 private static final int NO_POSITION = -1;
37 private LayoutInflater layoutInflater;
38 private OnItemClickListener<T> onItemClickListener;
40 private List<T> items = Collections.emptyList();
42 private T selectedItem;
43 private int selectedItemPosition = NO_POSITION;
45 private Selection selection = Selection.NONE;
47 private View.OnClickListener onClickListener = new View.OnClickListener() {
49 public void onClick(View v) {
50 int position = (int) v.getTag();
51 T item = getItem(position);
52 onItemClicked(v, item, position);
56 public RecyclerAdapter(Context context, Selection selection) {
57 layoutInflater = LayoutInflater.from(context);
58 this.selection = selection;
61 protected void onItemClicked(View view, T item, int position) {
62 selectItemAndNotify(item, position);
63 if (onItemClickListener != null) {
64 onItemClickListener.onItemClicked(item, view, position);
69 public void onBindViewHolder(VH viewHolder, final int position) {
70 viewHolder.itemView.setTag(position);
71 viewHolder.itemView.setOnClickListener(onClickListener);
72 T item = getItem(position);
73 viewHolder.itemView.setSelected(isSelected(getItem(position)));
74 viewHolder.bind(item, isSelected(item));
78 public int getItemCount() {
84 * @param onItemClickListener item click listener
86 public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
87 this.onItemClickListener = onItemClickListener;
91 public OnItemClickListener<T> getOnItemClickListener() {
92 return onItemClickListener;
95 public List<T> getItems() {
100 * @param items new items
102 public void setItems(List<T> items) {
104 notifyDataSetChanged();
107 public void addItems(List<T> items) {
108 this.items.addAll(items);
109 notifyDataSetChanged();
113 * @param position position of element to remove
114 * @return removed item or null if element not found
116 public T removeItem(int position) {
117 if (position >= 0 && position < items.size()) {
118 T item = items.remove(position);
119 notifyItemRemoved(position);
120 notifyItemRangeChanged(position, items.size());
126 public void clear() {
128 notifyDataSetChanged();
131 protected void addItem(int position, T item) {
132 items.add(position, item);
135 protected View inflate(int layoutResId, ViewGroup parent) {
136 return layoutInflater.inflate(layoutResId, parent, false);
140 * @param position position of element
141 * @return item from specific position
143 public T getItem(int position) {
144 return this.items.get(position);
147 protected abstract String getItemId(T item);
149 private boolean isSelected(T item) {
150 return selectedItem != null && getItemId(selectedItem).contains(getItemId(item));
153 public void selectItem(T item) {
154 int position = NO_POSITION;
155 for (int i = 0; i < items.size(); i++) {
156 if (getItemId(item).equals(getItemId(items.get(i)))) {
161 selectItemAndNotify(item, position);
164 private void selectItemAndNotify(T item, int position) {
165 int selectedPositionToNotify = selectedItemPosition;
166 setSelectedItem(item, position);
168 if (selectedPositionToNotify != NO_POSITION) {
169 notifyItemChanged(selectedPositionToNotify);
171 notifyItemChanged(position);
174 private void setSelectedItem(T item, int position) {
178 selectedItemPosition = position;