HFRecyclerView
This is an Android library allowing to add Header and/or Footer in your RecyclerView in the simplest way possible.
USAGE
To add Header and/or Footer in your RecyclerView you need to add HFRecyclerView library in your project or you can also grab it from Gradle:
compile 'com.mikhaellopez:hfrecyclerview:1.0.0'
JAVA
-
You need to create a custom
RecyclerView.Adapter
for your RecyclerView whichextends HFRecyclerView
with the object type of your choice (in my example, my object type isMyDataObject
:public class ExampleAdapter extends HFRecyclerView<MyDataObject> { //... }
-
In your custom adapter, add a constructor. The first param in
super
constructor is your data list. The second is a flag to determine if you want to add a header, and the last to add a footer.public class ExampleAdapter extends HFRecyclerView<MyDataObject> { public ExampleAdapter(List<MyDataObject> data) { // With Header & With Footer super(data, true, true); } //... }
-
After that, override 3 methods and create 3 class which extend
RecyclerView.ViewHolder
in order to add the viewHolder for your Item, your Header and your Footer:public class ExampleAdapter extends HFRecyclerView<MyDataObject> { //... //region Override Get ViewHolder @Override protected RecyclerView.ViewHolder getItemView(LayoutInflater inflater, ViewGroup parent) { return new ItemViewHolder(inflater.inflate(R.layout.item_example, parent, false)); } @Override protected RecyclerView.ViewHolder getHeaderView(LayoutInflater inflater, ViewGroup parent) { return new HeaderViewHolder(inflater.inflate(R.layout.item_header, parent, false)); } @Override protected RecyclerView.ViewHolder getFooterView(LayoutInflater inflater, ViewGroup parent) { return new FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false)); } //endregion //region ViewHolder Header and Footer class ItemViewHolder extends RecyclerView.ViewHolder { TextView text; public ItemViewHolder(View itemView) { super(itemView); text = (TextView)itemView.findViewById(R.id.text); } } class HeaderViewHolder extends RecyclerView.ViewHolder { public HeaderViewHolder(View itemView) { super(itemView); } } class FooterViewHolder extends RecyclerView.ViewHolder { public FooterViewHolder(View itemView) { super(itemView); } } //endregion }
-
Finally, you must override
onBindViewHolder
method to manage your views as you like:public class ExampleAdapter extends HFRecyclerView<MyDataObject> { //... @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ItemViewHolder) { ItemViewHolder itemViewHolder = (ItemViewHolder) holder; MyDataObject data = getItem(position); itemViewHolder.text.setText(data); } else if (holder instanceof HeaderViewHolder) { } else if (holder instanceof FooterViewHolder) { } } //... }
LICENCE
CircularImageView by Lopez Mikhael is licensed under a Apache License 2.0. Based on a work at @hister.