Created
November 13, 2017 22:10
-
-
Save zizibaloob/2eb64f63ba8d1468100a69997d525a54 to your computer and use it in GitHub Desktop.
Tiny app to demonstrate nested RecyclerViews
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
<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"/> |
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
<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> |
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
<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> |
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 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks It is very Helpful