Created
December 20, 2014 16:42
-
-
Save nschwermann/c76cf5b4832c47bc0375 to your computer and use it in GitHub Desktop.
RecyclerView HeaderAdapter
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
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.ViewGroup; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public abstract class HeaderAdapter extends RecyclerView.Adapter<ViewHolder> { | |
private static final String TAG = "HeaderAdapter"; | |
protected static final int HEADER = 0; | |
private List<BaseModel> mDataset = new ArrayList<BaseModel>(); | |
private List<ViewHolder> mHeaders = new ArrayList<ViewHolder>(); | |
//map of type to position | |
private Map<Integer, Integer> mHeaderTypes = new HashMap<Integer, Integer>(); | |
private ImageManager mImageManager; | |
private LayoutInflater mInflater; | |
private Context mContext; | |
public HeaderAdapter(Context context, ImageManager imageManager) { | |
mImageManager = imageManager; | |
mInflater = LayoutInflater.from(context); | |
mContext = context; | |
setHasStableIds(true); | |
} | |
public ImageManager getImageManager() { | |
return mImageManager; | |
} | |
public LayoutInflater getInflater() { | |
return mInflater; | |
} | |
public Context getContext() { | |
return mContext; | |
} | |
/** | |
* Setup major data set which would be a collection items | |
* */ | |
public void setDataset(List<BaseModel> dataset) { | |
mDataset = dataset; | |
notifyDataSetChanged(); | |
} | |
public List<BaseModel> getDataset(){ | |
return mDataset; | |
} | |
public void addHeader(ViewHolder header){ | |
mHeaders.add(header); | |
mHeaderTypes.put(getHeaderTypeForPosition(mHeaders.size() - 1), mHeaders.size() - 1); | |
notifyDataSetChanged(); | |
} | |
public void clearHeaders(){ | |
mHeaders.clear(); | |
mHeaderTypes.clear(); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public final ViewHolder onCreateViewHolder(final ViewGroup parent, final int type) { | |
if(type <= HEADER){ | |
final ViewHolder holder = mHeaders.get(mHeaderTypes.get(type)); | |
if(holder.itemView.getParent() != null){ | |
((ViewGroup)holder.itemView.getParent()).removeView(holder.itemView); | |
} | |
return holder; | |
} else { | |
return onCreateHolder(parent, type); | |
} | |
} | |
public abstract ViewHolder onCreateHolder(ViewGroup parent, int type); | |
@Override | |
public final void onBindViewHolder(final ViewHolder viewHolder, final int position) { | |
//headers don't need to be bound they static views unrelated to dataset. | |
if(!isHeader(position)){ | |
onBindHolder(viewHolder, (position - mHeaders.size())); | |
} | |
} | |
public abstract void onBindHolder(ViewHolder viewHolder, int position); | |
@Override | |
public int getItemCount() { | |
return mDataset.size() + mHeaders.size(); | |
} | |
public int getHeaderCount(){ | |
return mHeaders.size(); | |
} | |
@Override | |
public final int getItemViewType(final int position) { | |
if (isHeader(position)) { | |
return getHeaderTypeForPosition(position); | |
} else { | |
return getItemType((position - mHeaders.size())); | |
} | |
} | |
public abstract int getItemType(int position); | |
@Override | |
public long getItemId(int position) { | |
if(isHeader(position)){ | |
return position; //TODO something better to return? | |
} else { | |
return getIdForPos(position - mHeaders.size()); | |
} | |
} | |
public abstract long getIdForPos(int position); | |
/** | |
* Method returns true if position of item into headers set | |
* */ | |
private boolean isHeader(final int position) { | |
return !mHeaders.isEmpty() | |
&& position < mHeaders.size(); | |
} | |
private int getHeaderTypeForPosition(final int position){ | |
return HEADER - position; | |
} | |
@Override | |
public void onViewDetachedFromWindow(ViewHolder holder) { | |
super.onViewDetachedFromWindow(holder); | |
holder.onDetachedFromWindow(); | |
} | |
@Override | |
public void onViewAttachedToWindow(ViewHolder holder) { | |
super.onViewAttachedToWindow(holder); | |
holder.onAttachedToWindow(); | |
} | |
@Override | |
public void onViewRecycled(ViewHolder holder) { | |
super.onViewRecycled(holder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment