Skip to content

Instantly share code, notes, and snippets.

@prasannaboppe
Created November 10, 2016 17:43
Show Gist options
  • Save prasannaboppe/d34362da12f33c3d61fd2e2eeb7e157a to your computer and use it in GitHub Desktop.
Save prasannaboppe/d34362da12f33c3d61fd2e2eeb7e157a to your computer and use it in GitHub Desktop.
RecyclerView with Header and Footer
package com.prasanna.recyclerview.samples;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
/**
* Created by prasanna on 10/11/16.
*/
public class HeaderFooterRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<App> mApps;
// You can keep you own header and footer objects
private String mHeader;
private String mFooter;
private List<Integer> mActualPositions;
public interface OnItemClickListener {
void onHeaderClicked();
void onFooterClicked();
void onItemClicked(App app);
}
private OnItemClickListener mOnItemClickListener;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case R.layout.item_header:
return new HeaderFooterRecyclerAdapter.HeaderViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_header, parent, false));
case R.layout.item_footer:
return new HeaderFooterRecyclerAdapter.FooterViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_footer, parent, false));
case R.layout.item:
return new HeaderFooterRecyclerAdapter.ItemViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false));
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int pos = mActualPositions.get(position);
switch (pos) {
case R.layout.item_header:
HeaderViewHolder headerView = (HeaderViewHolder) holder;
headerView.textViewTitle.setText(mHeader);
break;
case R.layout.item_footer:
FooterViewHolder footerView = (FooterViewHolder) holder;
footerView.textViewTitle.setText(mFooter);
break;
default:
ItemViewHolder itemView = (ItemViewHolder) holder;
itemView.textViewTitle.setText(mApps.get(pos).title);
itemView.textViewRating.setText(mApps.get(pos).rating);
}
}
@Override
public int getItemCount() {
return mApps == null ? 0 : mApps.size() + 2;
}
@Override
public int getItemViewType(int position) {
if (position == 0) return R.layout.item_header;
if (position == mApps.size() + 1) return R.layout.item_footer;
return R.layout.item;
}
public void setData(List<App> apps, String header, String footer) {
mApps = apps;
int size = apps.size();
mActualPositions = new LinkedList<>();
mActualPositions.add(R.layout.item_header);
for (int i = 0; i < size; i++) mActualPositions.add(i);
mActualPositions.add(R.layout.item_footer);
mHeader = header;
mFooter = footer;
notifyDataSetChanged();
}
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}
private class HeaderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final TextView textViewTitle;
final TextView textViewSubTitle;
HeaderViewHolder(View itemView) {
super(itemView);
textViewTitle = (TextView) itemView.findViewById(R.id.text_view_title);
textViewSubTitle = (TextView) itemView.findViewById(R.id.text_view_sub_title);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mOnItemClickListener == null) return;
mOnItemClickListener.onHeaderClicked();
}
}
private class FooterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final TextView textViewTitle;
final TextView textViewSubTitle;
FooterViewHolder(View itemView) {
super(itemView);
textViewTitle = (TextView) itemView.findViewById(R.id.text_view_title);
textViewSubTitle = (TextView) itemView.findViewById(R.id.text_view_sub_title);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mOnItemClickListener == null) return;
mOnItemClickListener.onFooterClicked();
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final TextView textViewTitle;
final TextView textViewRating;
ItemViewHolder(View itemView) {
super(itemView);
textViewTitle = (TextView) itemView.findViewById(R.id.text_view_title);
textViewRating = (TextView) itemView.findViewById(R.id.text_view_sub_title);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mOnItemClickListener == null) return;
int pos = mActualPositions.get(getAdapterPosition());
mOnItemClickListener.onItemClicked(mApps.get(pos));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Jalebi - A Desi Word Game" />
<TextView
android:id="@+id/text_view_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="1M Downloads" />
</LinearLayout>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Games" />
<TextView
android:id="@+id/text_view_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Recommended for you" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment