Skip to content

Instantly share code, notes, and snippets.

@timusus
Last active October 9, 2015 03:58
Show Gist options
  • Save timusus/a43df394dc8e1c656e10 to your computer and use it in GitHub Desktop.
Save timusus/a43df394dc8e1c656e10 to your computer and use it in GitHub Desktop.
##BaseRecyclerAdapter##
package ${PACKAGE_NAME};
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder, I extends BaseRecyclerAdapter.OnItemClickListener<T>> extends RecyclerView.Adapter<VH> {
public interface OnItemClickListener<T> {
void onItemClick(int position, T item);
}
private final List<T> data = new ArrayList<>();
public I listener;
public void setData(List<T> data) {
this.data.clear();
this.data.addAll(data);
notifyDataSetChanged();
}
public T getItem(int position) {
return data.get(position);
}
@Override
public int getItemCount() {
return data.size();
}
public void setListener(I listener) {
this.listener = listener;
}
}
##RecyclerAdapter##
package ${PACKAGE_NAME};
import android.app.Notification;
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.List;
#parse("File Header.java")
public class ${NAME} extends BaseAdapter<${itemClass}, ${NAME}.ViewHolder, BaseAdapter.OnItemClickListener<${itemClass}>>{
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(${layoutName}, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
${itemClass} item = getItem(position);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
##Fragment##
package ${PACKAGE_NAME};
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
#parse("File Header.java")
public class ${NAME} extends Fragment
{
/**
* Empty constructor as per the {@link Fragment} docs
*/
public ${NAME}()
{
}
public static ${NAME} newInstance()
{
${NAME} fragment = new ${NAME}();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context context)
{
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(${layoutName}, container, false);
return rootView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment