Last active
November 10, 2016 17:31
-
-
Save prasannaboppe/07df69d4b504ab6d98b30249c4a668af to your computer and use it in GitHub Desktop.
RecyclerView with Headers (LinearLayout)
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
<?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> |
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
<?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> |
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
[ | |
{ | |
"title": "New + Updated Games", | |
"desc": "Selected games of the week", | |
"apps": [ | |
{ | |
"title": "Real Cricket", | |
"rating": "4.1" | |
}, | |
{ | |
"title": "Tower Crush", | |
"rating": "4.3" | |
}, | |
{ | |
"title": "Pool ZingPlay", | |
"rating": "4.7" | |
} | |
] | |
}, | |
{ | |
"title": "Casual Games", | |
"desc": "Recommended for you", | |
"apps": [ | |
{ | |
"title": "Gardens Capes", | |
"rating": "4.4" | |
}, | |
{ | |
"title": "Pen Fight", | |
"rating": "3.9" | |
} | |
] | |
}, | |
{ | |
"title": "You Might Like...", | |
"desc": "Based on your recent installs", | |
"apps": [ | |
{ | |
"title": "FIE Sword", | |
"rating": "4.6" | |
}, | |
{ | |
"title": "Mad Bullets", | |
"rating": "4.3" | |
}, | |
{ | |
"title": "50 50", | |
"rating": "4.1" | |
}, | |
{ | |
"title": "Sine Line", | |
"rating": "4.7" | |
}, | |
{ | |
"title": "Sine Line", | |
"rating": "4.7" | |
} | |
] | |
} | |
] |
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 Section { | |
public String title; | |
public String desc; | |
public List<App> apps; | |
public class App { | |
public String title; | |
public String rating; | |
} | |
} |
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
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 23/10/16. | |
*/ | |
public class SectionedRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private List<Section> mSections; | |
private List<Position> mActualPositions; | |
private OnChildItemClickListener mOnChildItemClickListener; | |
private OnHeaderClickListener mOnHeaderClickListener; | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
switch (viewType) { | |
case R.layout.item_header: | |
return new HeaderViewHolder(LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.item_header, parent, false)); | |
case R.layout.item: | |
return new ItemViewHolder(LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.item, parent, false)); | |
default: | |
return null; | |
} | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
Position pos = mActualPositions.get(position); | |
if (pos.child == -1) { | |
HeaderViewHolder headerView = (HeaderViewHolder) holder; | |
headerView.textViewTitle.setText(mSections.get(pos.header).title); | |
headerView.textViewSubTitle.setText(mSections.get(pos.header).desc); | |
} else { | |
ItemViewHolder itemView = (ItemViewHolder) holder; | |
itemView.textViewTitle.setText(mSections.get(pos.header).apps.get(pos.child).title); | |
itemView.textViewRating.setText(mSections.get(pos.header).apps.get(pos.child).rating); | |
} | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return isHeader(position) ? R.layout.item_header : R.layout.item; | |
} | |
@Override | |
public int getItemCount() { | |
if (mSections == null) return 0; | |
int count = 0; | |
count = mSections.size(); | |
for (Section section : mSections) { | |
count = count + section.apps.size(); | |
} | |
return count; | |
} | |
public void setData(List<Section> sections) { | |
mSections = sections; | |
int headers = sections.size(); | |
mActualPositions = new LinkedList<>(); | |
for (int i = 0; i < headers; i++) { | |
int size = mSections.get(i).apps.size(); | |
for (int j = -1; j < size; j++) { | |
mActualPositions.add(new Position(i, j)); | |
} | |
} | |
notifyDataSetChanged(); | |
} | |
private boolean isHeader(int pos) { | |
return mActualPositions.get(pos).child == -1; | |
} | |
public void setOnChildItemClickListener(OnChildItemClickListener listener) { | |
mOnChildItemClickListener = listener; | |
} | |
public void setOnHeaderClickListener(OnHeaderClickListener listener) { | |
mOnHeaderClickListener = listener; | |
} | |
private class Position { | |
int header; | |
int child; | |
Position(int header, int child) { | |
this.header = header; | |
this.child = child; | |
} | |
} | |
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 (mOnChildItemClickListener == null) return; | |
Position position = mActualPositions.get(getAdapterPosition()); | |
mOnChildItemClickListener.onChildItemClicked(position.header, position.child); | |
} | |
} | |
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 (mOnHeaderClickListener == null) return; | |
Position position = mActualPositions.get(getAdapterPosition()); | |
mOnHeaderClickListener.onHeaderClicked(position.header); | |
} | |
} | |
public interface OnChildItemClickListener { | |
void onChildItemClicked(int header, int child); | |
} | |
public interface OnHeaderClickListener { | |
void onHeaderClicked(int header); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment