animals = new ArrayList<>();
49 |
50 | animals.add(new Snake("Mub Adder", "Adder"));
51 | animals.add(new Snake("Texas Blind Snake", "Blind snake"));
52 | animals.add(new Snake("Tree Boa", "Boa"));
53 | animals.add(new Gecko("Fat-tailed", "Hemitheconyx"));
54 | animals.add(new Gecko("Stenodactylus", "Dune Gecko"));
55 | animals.add(new Gecko("Leopard Gecko", "Eublepharis"));
56 | animals.add(new Gecko("Madagascar Gecko", "Phelsuma"));
57 | animals.add(new UnknownReptile());
58 |
59 | animals.add(new Snake("Mub Adder", "Adder"));
60 | animals.add(new Snake("Texas Blind Snake", "Blind snake"));
61 | animals.add(new Snake("Tree Boa", "Boa"));
62 | animals.add(new Gecko("Fat-tailed", "Hemitheconyx"));
63 | animals.add(new Gecko("Stenodactylus", "Dune Gecko"));
64 | animals.add(new Gecko("Leopard Gecko", "Eublepharis"));
65 | animals.add(new Gecko("Madagascar Gecko", "Phelsuma"));
66 |
67 | animals.add(new Snake("Mub Adder", "Adder"));
68 | animals.add(new Snake("Texas Blind Snake", "Blind snake"));
69 | animals.add(new Snake("Tree Boa", "Boa"));
70 | animals.add(new Gecko("Fat-tailed", "Hemitheconyx"));
71 | animals.add(new Gecko("Stenodactylus", "Dune Gecko"));
72 | animals.add(new Gecko("Leopard Gecko", "Eublepharis"));
73 | animals.add(new Gecko("Madagascar Gecko", "Phelsuma"));
74 |
75 | Collections.shuffle(animals);
76 | return animals;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/library/src/main/java/com/hannesdorfmann/adapterdelegates4/AbsListItemAdapterDelegate.java:
--------------------------------------------------------------------------------
1 | package com.hannesdorfmann.adapterdelegates4;
2 |
3 | import android.view.ViewGroup;
4 |
5 | import java.util.List;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.recyclerview.widget.RecyclerView;
9 |
10 | /**
11 | * A simplified {@link AdapterDelegate} when the underlying adapter's dataset is a {@linkplain
12 | * List}. This class helps to reduce writing boilerplate code like casting list item and casting
13 | * ViewHolder.
14 | *
15 | *
16 | * For instance if you have a list of animals (different kind of animals in classes like Cat, Dog
17 | * etc. assuming all have a common super class Animal) you want to display in your adapter and
18 | * you want to create a CatAdapterDelegate then this class would look like this:
19 | * {@code
20 | * class CatAdapterDelegate extends AbsListItemAdapterDelegate{
21 | *
22 | * @param The type of the item that is managed by this AdapterDelegate. Must be a subtype of T
23 | * @param The generic type of the list, in other words: {@code List}
24 | * @param The type of the ViewHolder
25 | * @author Hannes Dorfmann
26 | * @Override protected boolean isForViewType(Animal item, List items, position){
27 | * return item instanceof Cat;
28 | * }
29 | * @Override public CatViewHolder onCreateViewHolder(ViewGroup parent){
30 | * return new CatViewHolder(inflater.inflate(R.layout.item_cat, parent, false));
31 | * }
32 | * @Override protected void onBindViewHolder(Cat item, CatViewHolder holder);
33 | * holder.setName(item.getName());
34 | * ...
35 | * }
36 | * }
37 | *
38 | * }
39 | *
40 | * @since 1.2
41 | */
42 | public abstract class AbsListItemAdapterDelegate
43 | extends AdapterDelegate> {
44 |
45 | @Override
46 | protected final boolean isForViewType(@NonNull List items, int position) {
47 | return isForViewType(items.get(position), items, position);
48 | }
49 |
50 | @Override
51 | protected final void onBindViewHolder(@NonNull List items, int position,
52 | @NonNull RecyclerView.ViewHolder holder, @NonNull List