Skip to content

Instantly share code, notes, and snippets.

@ec84b4
Last active June 26, 2018 06:22
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save ec84b4/d56c00fb5fd2dfaf279b to your computer and use it in GitHub Desktop.
Save ec84b4/d56c00fb5fd2dfaf279b to your computer and use it in GitHub Desktop.
recycler view header adapter
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by khaled bakhtiari on 10/26/2014.
* <a href="http://about.me/kh.bakhtiari">
*/
public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
String[] data;
public HeaderAdapter(String[] data) {
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
//inflate your layout and pass it to view holder
return new VHItem(null);
} else if (viewType == TYPE_HEADER) {
//inflate your layout and pass it to view holder
return new VHHeader(null);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof VHItem) {
String dataItem = getItem(position);
//cast holder to VHItem and set data
} else if (holder instanceof VHHeader) {
//cast holder to VHHeader and set data for header.
}
}
@Override
public int getItemCount() {
return data.length + 1;
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
private String getItem(int position) {
return data[position - 1];
}
class VHItem extends RecyclerView.ViewHolder {
TextView title;
public VHItem(View itemView) {
super(itemView);
}
}
class VHHeader extends RecyclerView.ViewHolder {
Button button;
public VHHeader(View itemView) {
super(itemView);
}
}
}
@jpshelley
Copy link

I would just add that if you don't want the header/footer to be displayed if the data set is empty to change:

    @Override
    public int getItemCount() {
       if (data.isEmpty() {
          return data.length;
       } else {
           return data.length + 1;
        }
    }

@evansch
Copy link

evansch commented Jan 14, 2015

I am trying to implement with a footer only. The part i'm confused about is how do I tell if the position is the footer? It makes sense with a header because its at position 0, but the position of my footer will keep changing. Any thoughts? Thanks!

@ec84b4
Copy link
Author

ec84b4 commented Jan 18, 2015

position for footer is always getItemCount() -1 (meaning always the las item)

@rafiqlightwala
Copy link

Hi, hister. I successfully used this to create a header (which is basically a refresh progressbar). The issue is I want to hide this header (GONE treatment) but can't seem to make this work. Any suggestion?

@Ruud-cb
Copy link

Ruud-cb commented Sep 28, 2015

@jpshelley, I think you ment the other way around:

    @Override
    public int getItemCount() {
       if (data.isEmpty()) {
          return data.length + 1;
       } else {
           return data.length;
        }
    }

This will show the header if the data is empty.

@mmanishh
Copy link

mmanishh commented Nov 6, 2015

What to do if i dnt want header for one Activity and want header in another activity with same adapter

@ec84b4
Copy link
Author

ec84b4 commented Nov 14, 2015

@rafiqlightwala i've had the same problem what i did to solve the issue was to set visibility to gone on every child of the footer (i had a refresh progressbar as well, with a textview and a button)

@ec84b4
Copy link
Author

ec84b4 commented Nov 14, 2015

@mmanishh you could easily send a flag to show the footer or not
or you could have a inheritance of the adapter that doesn't show the footer of course you'd need to tweak the code a bit

@mmanishh
Copy link

mmanishh commented Dec 3, 2015

The first item for Recyclerview is not displayed . Can u tell why ? and the solution too

@xAIdrian
Copy link

How can we implement this with multiple Headers?

@dpux
Copy link

dpux commented Apr 18, 2016

Is there a way we can add Accordion behavior to this ?

@mainul-hossain
Copy link

I have been followed above example but In my case, It only shows the header and rest of items showing blank. I have checked that logs showing the data but It's not visible in Recyclerview rows. Can anyone tell me why it's happening?
recyclerview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment