Created
February 15, 2015 11:16
-
-
Save mzgreen/e95e138cfd95d955949c to your computer and use it in GitHub Desktop.
HideOnScrollExample - RecyclerAdapter class
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
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
//added view types | |
private static final int TYPE_HEADER = 2; | |
private static final int TYPE_ITEM = 1; | |
private List<String> mItemList; | |
public RecyclerAdapter(List<String> itemList) { | |
mItemList = itemList; | |
} | |
//modified creating viewholder, so it creates appropriate holder for a given viewType | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
Context context = parent.getContext(); | |
if (viewType == TYPE_ITEM) { | |
final View view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false); | |
return RecyclerItemViewHolder.newInstance(view); | |
} else if (viewType == TYPE_HEADER) { | |
final View view = LayoutInflater.from(context).inflate(R.layout.recycler_header, parent, false); | |
return new RecyclerHeaderViewHolder(view); | |
} | |
throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly"); | |
} | |
//modifed ViewHolder binding so it binds a correct View for the Adapter | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { | |
if (!isPositionHeader(position)) { | |
RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder; | |
String itemText = mItemList.get(position - 1); // we are taking header in to account so all of our items are correctly positioned | |
holder.setItemText(itemText); | |
} | |
} | |
//our old getItemCount() | |
public int getBasicItemCount() { | |
return mItemList == null ? 0 : mItemList.size(); | |
} | |
//our new getItemCount() that includes header View | |
@Override | |
public int getItemCount() { | |
return getBasicItemCount() + 1; // header | |
} | |
//added a method that returns viewType for a given position | |
@Override | |
public int getItemViewType(int position) { | |
if (isPositionHeader(position)) { | |
return TYPE_HEADER; | |
} | |
return TYPE_ITEM; | |
} | |
//added a method to check if given position is a header | |
private boolean isPositionHeader(int position) { | |
return position == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment