英文:
What to use instead of RecyclerView if not recycling items
问题
我是一个初学者,我已经看过许多关于如何使用RecyclerView
创建列表的教程,但我找不到任何关于如何在不进行回收的情况下使用列表的教程。我有一个包含3个项目的列表。如果我不想进行回收,我应该使用什么类型的视图?
英文:
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?
答案1
得分: 4
另一种查看您的列表的方法是将其视为简单的项目(因为只有3个)。您可以将项目添加到具有垂直方向的LinearLayout
中。
您甚至可以进一步创建一个通用的项目布局XML,并使用for循环填充您的LinearLayout
。示例:
//创建项目类
@AllArgsConstructor @Getter
private static class Item {
private int iconId;
private String mainText;
private String detailsText;
}
//创建项目
private Item ITEM_1 = new Item(R.drawable.some_drawable, getString(R.string.some_string), getString(R.string.some_string));
//将项目添加到ArrayList中(只添加您希望已登录用户看到的内容:D)
itemList.add(ITEM_1)
//要添加的布局
@BindView(R.id.content) LinearLayout layoutToAddTo;
LayoutInflater inflater = LayoutInflater.from(getContext());
for (Item item : itemList) { // 保存所有项目的列表
//与Item类匹配的XML。
View card = inflater.inflate(R.layout.card, layoutToAddTo, false);
bindContent(card, item);
card.setOnClickListener(__ -> /*设置您的侦听器*/));
layoutToAddTo.addView(card);
}
//绑定您的内容
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());
}
对于少量项目来说是最佳选择,否则请尝试使用**Recycler View**。
英文:
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.
答案2
得分: 2
你可以使用ListView
,如果你不想回收列表项。
在这里查看ListView。
英文:
You can use ListView
if you don't want to recycle the list item.
Here ListView
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论