Skip to content

Instantly share code, notes, and snippets.

@samigehi
Created November 22, 2018 09:34
Show Gist options
  • Save samigehi/8b52dbf3222603ff8c28457905cc9c70 to your computer and use it in GitHub Desktop.
Save samigehi/8b52dbf3222603ff8c28457905cc9c70 to your computer and use it in GitHub Desktop.
Simpler generic RecyclerView.Adapter and RecyclerView.ViewHolder with click listener, easy to use, no boilerplate code
package com.sumeet.gehi;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RecyclerAdapter<A extends RecyclerAdapter.ITEM> extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<A> items;
private Context context;
private int layout = 0;
private OnItemClick<A> listener;
public RecyclerAdapter(Context context, int layoutId, OnItemClick<A> listener) {
this(context, layoutId, new ArrayList<>(), listener);
}
public RecyclerAdapter(Context context, int layoutId, List<A> list, OnItemClick<A> listener) {
super();
this.context = context;
this.listener = listener;
this.items = list;
this.layout = layoutId;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(layout, viewGroup, false), listener);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
A a = getItem(position);
if (a != null)
a.setData(holder);
}
@Override
public int getItemCount() {
return items.size();
}
public A getItem(int index) {
return ((items != null && index < items.size()) ? items.get(index) : null);
}
public void addAll(List<A> list) {
if (items == null)
items = new ArrayList<A>();
items.addAll(list);
notifyDataSetChanged();
}
public void reset() {
items.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Map<Integer, View> views;
OnItemClick<A> listener;
public ViewHolder(View view, OnItemClick<A> listener) {
super(view);
views = new HashMap<>();
views.put(0, view);
this.listener = listener;
if (listener != null)
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (listener != null)
listener.onClick(getItem(getAdapterPosition()), getAdapterPosition());
}
private void initViewById(int id) {
View view = (getView() != null ? getView().findViewById(id) : null);
if (view != null)
views.put(id, view);
}
public View getView() {
return getView(0);
}
public View getView(int id) {
Logger.e("RecyclerAdapter", "View getView " +id);
if (views.containsKey(id))
return views.get(id);
else
initViewById(id);
return views.get(id);
}
}
public interface OnItemClick<A> {
public void onClick(A item, int pos);
}
public static abstract class ITEM {
abstract public void setData(RecyclerAdapter.ViewHolder holder);
}
}
// in Activity class ....
recyclerView.setLayoutManager(...)
recyclerView.setAdapter(new RecyclerAdapter<Item>(context, R.layout.list_item, list, listner{
})
public class Item extends RecyclerAdapter.ITEM {
public String name;
public String type;
public String desc;
public boolean clickable = true
public Item(String n, String d, String t) {
super();
name = n;
desc = d;
type = t;
}
@Override
pubilc void setData(ViewHolder holder){
(TextView holder.getView(R.id.tv_title)).setText(name);
(TextView holder.getView(R.id.tv_desc)).setText(desc);
(TextView holder.getView(R.id.tv_type)).setText(type);
...
}
}
//or in kotlin
val list = ArrayList<Items>()
list.add(Items(....))
.
.
.
val adapter = RecyclerAdapter<Items>(this, R.layout.list_item, list, { item, position ->
});
recyclerView.layoutManager = LinearLayoutManager(this)
val horizontalDecoration = DividerItemDecoration(recyclerView.context, DividerItemDecoration.VERTICAL)
recyclerView.addItemDecoration(horizontalDecoration)
// set adapter
recyclerView.adapter = adapter;
class Items(val name: String, val desc: String, val type: String?) : RecyclerAdapter.ITEM() {
override fun setData(holder: RecyclerAdapter<*>.ViewHolder?) {
(holder?.getView(R.id.tv_title) as TextView?)?.text = name
(holder?.getView(R.id.tv_desc) as TextView?)?.text = desc
(holder?.getView(R.id.tv_type) as TextView?)?.text = type
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment