-
-
Save rohitnotes/4a3892a156c2d3d63dc49ed39fda258b to your computer and use it in GitHub Desktop.
BaseRecyclerViewAdapter Android
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
/** | |
* @author Yuana andhikayuana@gmail.com | |
* @since Jun, Sun 10 2018 13.44 | |
**/ | |
public abstract class BaseAdapter<T, V extends BaseViewHolder> extends RecyclerView.Adapter<V> { | |
protected List<T> data = new ArrayList<T>(); | |
protected Context context; | |
protected OnItemClickListener itemClickListener; | |
protected OnLongItemClickListener longItemClickListener; | |
public BaseAdapter(Context context) { | |
this.context = context; | |
} | |
public void setOnItemClickListener(OnItemClickListener itemClickListener) { | |
this.itemClickListener = itemClickListener; | |
} | |
public void setOnLongItemClickListener(OnLongItemClickListener longItemClickListener) { | |
this.longItemClickListener = longItemClickListener; | |
} | |
@Override | |
public int getItemCount() { | |
return data.size(); | |
} | |
public T get(int position) { | |
return data.get(position); | |
} | |
public void replaceData(List<T> items) { | |
data.clear(); | |
add(items); | |
} | |
public void add(T item) { | |
data.add(item); | |
notifyItemInserted(data.size() - 1); | |
} | |
public void add(T item, int position) { | |
data.add(position, item); | |
notifyItemInserted(position); | |
} | |
public void add(List<T> items) { | |
for (T item : items) { | |
data.add(item); | |
} | |
notifyDataSetChanged(); | |
} | |
public void add(T[] items) { | |
add(Arrays.asList(items)); | |
notifyDataSetChanged(); | |
} | |
public void addOrUpdate(T item) { | |
int i = data.indexOf(item); | |
if (i >= 0) { | |
data.set(i, item); | |
notifyItemChanged(i); | |
} else { | |
add(item); | |
} | |
} | |
public void addOrUpdate(List<T> items) { | |
for (T item : items) { | |
addOrUpdate(item); | |
} | |
notifyDataSetChanged(); | |
} | |
public void remove(int position) { | |
if (position >= 0 && position < data.size()) { | |
data.remove(position); | |
notifyItemRemoved(position); | |
} | |
} | |
public void remove(T item) { | |
int position = data.indexOf(item); | |
remove(position); | |
} | |
public void clear() { | |
data.clear(); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public V onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(setItemView(viewType), parent, false); | |
return initViewHolder(view, viewType); | |
} | |
protected abstract V initViewHolder(View view, int viewType); | |
@Override | |
public void onBindViewHolder(V holder, int position) { | |
holder.bind(get(position)); | |
} | |
@LayoutRes | |
protected abstract int setItemView(int viewType); | |
} |
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
/** | |
* @author Yuana andhikayuana@gmail.com | |
* @since Jul, Sun 22 2018 17.34 | |
**/ | |
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder implements | |
View.OnClickListener, | |
View.OnLongClickListener { | |
private OnItemClickListener itemClickListener; | |
private OnLongItemClickListener longItemClickListener; | |
public BaseViewHolder(View itemView) { | |
super(itemView); | |
} | |
public BaseViewHolder(View itemView, | |
OnItemClickListener itemClickListener, | |
OnLongItemClickListener longItemClickListener) { | |
super(itemView); | |
this.itemClickListener = itemClickListener; | |
this.longItemClickListener = longItemClickListener; | |
itemView.setOnClickListener(this); | |
itemView.setOnLongClickListener(this); | |
} | |
public abstract void bind(T item); | |
@Override | |
public void onClick(View v) { | |
if (itemClickListener != null) { | |
itemClickListener.onItemClick(v, getAdapterPosition()); | |
} | |
} | |
@Override | |
public boolean onLongClick(View v) { | |
if (longItemClickListener != null) { | |
longItemClickListener.onLongItemClick(v, 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
/** | |
* @author Yuana andhikayuana@gmail.com | |
* @since Jul, Sun 22 2018 17.34 | |
**/ | |
public interface OnItemClickListener { | |
void onItemClick(View view, int position); | |
} |
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
/** | |
* @author Yuana andhikayuana@gmail.com | |
* @since Jul, Sun 22 2018 17.34 | |
**/ | |
public interface OnLongItemClickListener { | |
void onLongItemClick(View view, int position); | |
} |
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
/** | |
* @author Yuana andhikayuana@gmail.com | |
* @since Jun, Sun 10 2018 13.45 | |
**/ | |
public abstract class SortedAdapter<T, H extends BaseViewHolder> extends RecyclerView.Adapter<H> { | |
// TODO: 11/06/18 | |
protected Context context; | |
protected SortedList<T> data; | |
protected OnItemClickListener itemClickListener; | |
protected OnLongItemClickListener longItemClickListener; | |
public SortedAdapter(Context context) { | |
this.context = context; | |
data = new SortedList<>(getItemClass(), new SortedList.Callback<T>() { | |
@Override | |
public int compare(T lhs, T rhs) { | |
return SortedAdapter.this.compare(lhs, rhs); | |
} | |
@Override | |
public void onInserted(int position, int count) { | |
} | |
@Override | |
public void onRemoved(int position, int count) { | |
} | |
@Override | |
public void onMoved(int fromPosition, int toPosition) { | |
notifyItemMoved(fromPosition, toPosition); | |
notifyItemChanged(toPosition); | |
} | |
@Override | |
public void onChanged(int position, int count) { | |
} | |
@Override | |
public boolean areContentsTheSame(T oldItem, T newItem) { | |
return oldItem.equals(newItem); | |
} | |
@Override | |
public boolean areItemsTheSame(T oldItem, T newItem) { | |
return oldItem.equals(newItem); | |
} | |
}); | |
} | |
protected abstract Class<T> getItemClass(); | |
protected abstract int compare(T lhs, T rhs); | |
protected View getView(ViewGroup parent, int viewType) { | |
return LayoutInflater.from(context).inflate(getItemResourceLayout(viewType), parent, false); | |
} | |
protected abstract int getItemResourceLayout(int viewType); | |
@Override | |
public abstract H onCreateViewHolder(ViewGroup parent, int viewType); | |
@Override | |
public void onBindViewHolder(H holder, int position) { | |
holder.bind(data.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
try { | |
return data.size(); | |
} catch (Exception e) { | |
return 0; | |
} | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
public void setOnItemClickListener(OnItemClickListener itemClickListener) { | |
this.itemClickListener = itemClickListener; | |
} | |
public void setOnLongItemClickListener(OnLongItemClickListener longItemClickListener) { | |
this.longItemClickListener = longItemClickListener; | |
} | |
public SortedList<T> getData() { | |
return data; | |
} | |
public T get(int position) { | |
return getData().get(position); | |
} | |
public boolean isEmpty() { | |
return data.size() < 1; | |
} | |
public int add(T item) { | |
int i = data.add(item); | |
notifyItemInserted(i); | |
return i; | |
} | |
public void add(final List<T> items) { | |
data.addAll(items); | |
notifyDataSetChanged(); | |
} | |
public void addOrUpdate(T item) { | |
int i = findPosition(item); | |
if (i >= 0) { | |
data.updateItemAt(i, item); | |
notifyItemChanged(i); | |
} else { | |
add(item); | |
} | |
} | |
public void addOrUpdate(final List<T> items) { | |
for (T item : items) { | |
int i = findPosition(item); | |
if (i >= 0) { | |
data.updateItemAt(i, item); | |
} else { | |
data.add(item); | |
} | |
} | |
notifyDataSetChanged(); | |
} | |
public void refreshWithData(List<T> items) { | |
data.clear(); | |
data.addAll(items); | |
notifyDataSetChanged(); | |
} | |
public void remove(int position) { | |
if (position >= 0 && position < data.size()) { | |
data.removeItemAt(position); | |
notifyItemRemoved(position); | |
} | |
} | |
public void remove(T item) { | |
int position = findPosition(item); | |
remove(position); | |
} | |
public void clear() { | |
data.clear(); | |
} | |
public int findPosition(T item) { | |
if (data == null) { | |
return -1; | |
} | |
int size = data.size() - 1; | |
for (int i = size; i >= 0; i--) { | |
if (data.get(i).equals(item)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment