Created
April 24, 2017 15:50
-
-
Save demixdn/810d6abef80063894122ced0d052e4e7 to your computer and use it in GitHub Desktop.
Simple Recycler Adapter with generics
This file contains hidden or 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
import android.support.annotation.LayoutRes; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.List; | |
/** | |
* Created on 24.04.2017 | |
* Project RecyclerBind | |
* | |
* @author Aleks Sander | |
*/ | |
public abstract class SimpleAdapter<T> extends RecyclerView.Adapter<SimpleHolder<T>> { | |
private final List<T> items; | |
@LayoutRes | |
private final int layoutId; | |
public SimpleAdapter(@Nullable List<T> items, @LayoutRes int layoutId) { | |
this.items = items; | |
this.layoutId = layoutId; | |
} | |
@Override | |
public SimpleHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) { | |
View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); | |
return createHolder(itemView); | |
} | |
@Override | |
public void onBindViewHolder(SimpleHolder<T> holder, int position) { | |
if (items != null) { | |
holder.bindModel(items.get(position)); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return items == null ? 0 : items.size(); | |
} | |
public abstract SimpleHolder<T> createHolder(View itemView); | |
} |
This file contains hidden or 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
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
/** | |
* Created on 24.04.2017 | |
* Project RecyclerBind | |
* | |
* @author Aleks Sander | |
*/ | |
public abstract class SimpleHolder<T> extends RecyclerView.ViewHolder { | |
public SimpleHolder(View itemView) { | |
super(itemView); | |
} | |
public abstract void bindModel(T t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment