Skip to content

Instantly share code, notes, and snippets.

@zizibaloob
Created November 13, 2017 22:10
Show Gist options
  • Save zizibaloob/2eb64f63ba8d1468100a69997d525a54 to your computer and use it in GitHub Desktop.
Save zizibaloob/2eb64f63ba8d1468100a69997d525a54 to your computer and use it in GitHub Desktop.
Tiny app to demonstrate nested RecyclerViews
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
tools:text="title"/>
<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="80dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recycler = findViewById(R.id.list);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.setAdapter(new CategoriesAdapter());
}
private static class CategoriesAdapter extends RecyclerView.Adapter<CategoryViewHolder> {
@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.category, parent, false);
return new CategoryViewHolder(itemView);
}
@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
holder.title.setText("category: " + position);
holder.recycler.setAdapter(new ItemsAdapter());
}
@Override
public int getItemCount() {
return 100;
}
}
private static class CategoryViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final RecyclerView recycler;
private CategoryViewHolder(View itemView) {
super(itemView);
this.title = itemView.findViewById(R.id.title);
this.recycler = itemView.findViewById(R.id.list);
recycler.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
}
}
private static class ItemsAdapter extends RecyclerView.Adapter<ItemViewHolder> {
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item, parent, false);
return new ItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.title.setText("item: " + position);
int number = (position % 10) + 1;
String url = "http://lorempixel.com/400/200/cats/" + number + "/";
Picasso.with(holder.itemView.getContext())
.load(url)
.into(holder.image);
}
@Override
public int getItemCount() {
return 50;
}
}
private static class ItemViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final ImageView image;
private ItemViewHolder(View itemView) {
super(itemView);
this.title = itemView.findViewById(R.id.title);
this.image = itemView.findViewById(R.id.image);
}
}
}
@dawitadmassu
Copy link

Thanks It is very Helpful

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