Created
August 1, 2015 01:00
-
-
Save vijaysharm/2442274b40d81ded596a to your computer and use it in GitHub Desktop.
Sectioned RecyclerView
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 Example { | |
private static final class SectionViewHolder extends RecyclerView.ViewHolder { | |
private final TextView title; | |
public SectionViewHolder(View view) { | |
super(view); | |
title = (TextView)view.findViewById(R.id.title); | |
} | |
public void bind(String section) { | |
title.setText(section); | |
} | |
} | |
private static final class RowViewHolder extends RecyclerView.ViewHolder { | |
private final TextView title; | |
public RowViewHolder(View view) { | |
super(view); | |
title = (TextView)view.findViewById(R.id.title); | |
} | |
public void bind(Data data) { | |
title.setText(data.label); | |
} | |
} | |
private static class DataAdapter extends SectionedRecyclerViewAdapter { | |
private final List<List<Data>> data; | |
private final Context context; | |
private final LayoutInflater inflater; | |
public DataAdapter(Context context) { | |
this.context = context; | |
this.inflater = LayoutInflater.from(context); | |
this.data = new ArrayList<>(); | |
} | |
public void setData(List<List<Data>> data) { | |
this.data.clear(); | |
this.data.addAll(data); | |
notifyDataSetChanged(); | |
} | |
@Override | |
protected int numberOfSections() { | |
return data.size(); | |
} | |
@Override | |
protected int numberOfRowsInSection(int section) { | |
return data.get(section).size(); | |
} | |
@Override | |
protected RecyclerView.ViewHolder onCreateSectionViewHolder(ViewGroup parent) { | |
View view = inflater.inflate(R.layout.section, parent, false); | |
return new SectionViewHolder(view); | |
} | |
@Override | |
protected RecyclerView.ViewHolder onCreateRowViewHolder(ViewGroup parent) { | |
View view = inflater.inflate(R.layout.row, parent, false); | |
return new RowViewHolder(view); | |
} | |
@Override | |
protected void onBindSectionViewHolder(RecyclerView.ViewHolder holder, int section) { | |
SectionViewHolder viewHolder = (SectionViewHolder)holder; | |
viewHolder.bind(data.get(section).domain); | |
} | |
@Override | |
protected void onBindRowViewHolder(RecyclerView.ViewHolder holder, int section, int row) { | |
RowViewHolder viewHolder = (RowViewHolder)holder; | |
viewHolder.bind(data.get(section).get(row)); | |
} | |
} | |
} |
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.support.v7.widget.RecyclerView; | |
import android.view.ViewGroup; | |
public abstract class SectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
@Override | |
final public int getItemCount() { | |
int count = numberOfSections(); | |
for (int section = 0; section < numberOfSections(); section++) { | |
count += numberOfRowsInSection(section); | |
} | |
return count; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return isSectionHeader(position) ? 0 : 1; | |
} | |
@Override | |
final public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return viewType == 0 ? onCreateSectionViewHolder(parent) : onCreateRowViewHolder(parent); | |
} | |
@Override | |
final public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
int section = getSection(position); | |
if (isSectionHeader(position)) { | |
onBindSectionViewHolder(holder, section); | |
} else { | |
int row = getRow(position); | |
onBindRowViewHolder(holder, section, row); | |
} | |
} | |
protected abstract int numberOfSections(); | |
protected abstract int numberOfRowsInSection(int section); | |
protected abstract RecyclerView.ViewHolder onCreateSectionViewHolder(ViewGroup parent); | |
protected abstract RecyclerView.ViewHolder onCreateRowViewHolder(ViewGroup parent); | |
protected abstract void onBindSectionViewHolder(RecyclerView.ViewHolder holder, int section); | |
protected abstract void onBindRowViewHolder(RecyclerView.ViewHolder holder, int section, int row); | |
protected boolean isSectionHeader(int position) { | |
int current = 0; | |
for (int section = 0; section < numberOfSections(); section++) { | |
if (current == position) return true; | |
current = (current + 1 + numberOfRowsInSection(section)); | |
} | |
return false; | |
} | |
private int getSection(int position) { | |
int current = 0; | |
for (int section = 0; section < numberOfSections(); section++) { | |
int end = (current + 1 + numberOfRowsInSection(section)); | |
if (position >= current && position < end) | |
return section; | |
current = end; | |
} | |
throw new IllegalArgumentException("Position " + position + " is not part of a valid section"); | |
} | |
private int getRow(int position) { | |
int current = 0; | |
for (int section = 0; section < numberOfSections(); section++) { | |
int end = (current + 1 + numberOfRowsInSection(section)); | |
if (position > current && position < end) | |
return (position - current - 1); | |
current = end; | |
} | |
throw new IllegalArgumentException("Position " + position + " does not point to a row item"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, I'm missing touch handlers, which I'll add in another revision.