I am a beginner and I have seen many tutorials on how to create a list using RecyclerView
, but I can't find any how to use a list without recycling. I have a list of 3 items. what type of view should I use if I don't want to recycle?
Asked
Active
Viewed 698 times
0

Afsar edrisy
- 1,985
- 12
- 28

DIRTY DAVE
- 2,523
- 2
- 20
- 83
-
you can use [listview](https://www.tutorialspoint.com/android/android_list_view.htm) insted of recycle view. – Kanzariya Hitesh Jan 03 '20 at 11:23
-
ListView was the original View provided for list item screen. For memory management point of view ListView with ViewHolder pattern was used but it had its own drawback. Finally Google came up with RecyclerView. As per my understanding irrespective of list size you should use RecyclerView. First you need to understand when will recycling help? – Shadow Droid Jan 03 '20 at 11:25
2 Answers
4
Another way to look at you list would be as simple items (since there are only 3). You can just add items to a LinearLayout
with orientation as vertical
.
You can even go further and create a common item layout XML, and using a for loop, inflate your LinearLayout
. Example:
//create item class
@AllArgsConstructor @Getter
private static class Item {
private int iconId;
private String mainText;
private String detailsText;
}
// create item
private Item ITEM_1 = new Item(R.drawable.some_drawable, getString(R.string.some_string), getString(R.string.some_string));
//add item to an arrayList (only add what you want that logged in user to see :D)
itemList.add((ITEM_1))
//layout you want to add to
@BindView(R.id.content) LinearLayout layoutToAddTo;
LayoutInflater inflater = LayoutInflater.from(getContext());
for (Item item : itemList) { // a list which holds all your Items
//XML that the Item class matches.
View card = inflater.inflate(R.layout.card, layoutToAddTo, false);
bindContent(card, item);
card.setOnClickListener(__ -> /* set your listener */));
layoutToAddTo.addView(card);
}
//bind your content
private void bindContent(View view, Item item) {
((ImageView) view.findViewById(R.id.card_icon)).setImageDrawable(ContextCompat.getDrawable(context, item.getIconId());
((TextView) view.findViewById(R.id.card_main_text)).setText(item.getMainText());
((TextView) view.findViewById(R.id.card_details_text)).setText(item.getDetailsText());
}
Best for few items, otherwise try to use Recycler View.

Darvesh
- 56
- 1
- 3
2
You can use ListView
if you don't want to recycle the list item.
Here ListView

Afsar edrisy
- 1,985
- 12
- 28
-
But this question say ListView also recycler? https://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – DIRTY DAVE Jan 03 '20 at 11:25
-
have a look on it [LRecyclerView vs. ListView](https://stackoverflow.com/questions/26728651/recyclerview-vs-listview) – Afsar edrisy Jan 03 '20 at 11:31
-
If you have fixed three items & items will not increase in your app any time, then batter not to use any List type instead use import ```xml``` three times you import design ```xml``` file in your main ```xml``` – Afsar edrisy Jan 03 '20 at 11:36