我应该如何为大型 ListView 列表(+100 个项目)定义图标
How should I define icons for a large ListView list (+100 items)
我一直在尝试创建一个 ListView,并且我已经成功创建了一个。但是,我的 ListView 现在对列表中的每个项目使用相同的图标。当我一直在寻找答案时,人们只是展示了少于 10 个项目的列表示例。但是我的列表包含超过 100 个项目。
对于文本,我刚刚在 string.xml 中创建了一个字符串数组,并使用
检索了它们
Resources res = getResources();
String[] list_of_names = res.getStringArray(R.array.names);
但是我如何对图标做同样的事情呢? Can/Should 我在单独的文件中定义它们,就像我对字符串数组所做的那样?我的图标在我的可绘制文件夹中。
此外,我是否应该为我的列表图标使用一组图像?还有别的办法吗?
感谢帮助!
编辑:
CustomAdapter.java
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] listNames) {
super(context, R.layout.list_item_copy, listNames);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.list_item_copy, parent, false);
String item = getItem(position);
TextView textView = (TextView) customView.findViewById(R.id.name);
ImageView imageView = (ImageView) customView.findViewById(R.id.icon);
textView.setText(item);
imageView.setImageResource(R.drawable.icon_red);
return customView;
}
}
MainActivity.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Resources res = getResources();
String[] listNames = res.getStringArray(R.array.names);
ArrayAdapter<String> adapter = new CustomAdapter(getActivity(), listNames);
ListView list = (ListView) rootView.findViewById(R.id.nameList);
list.setAdapter(adapter);
return rootView;
}
这会创建一个所有项目都具有相同图标的列表,但图标应该不同。
如果有什么不同,我已经遵循了新波士顿的教程:http://youtu.be/nOdSARCVYic?list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
你可以在strings.xml里面定义这样:
<array name="drawerIcons">
<item>@drawable/ic_action_design</item>
<item>@drawable/ic_action_develop</item>
</array>
您可以将它添加到您的 ArrayList 中:
ArrayList<DrawerItem> drawerItems = new ArrayList<>();
final String[] drawerTitles = getResources().getStringArray(R.array.drawer);
final TypedArray drawerIcons = getResources().obtainTypedArray(R.array.drawerIcons);
for (int i = 0; i < drawerTitles.length; i++) {
drawerItems.add(new DrawerItem(drawerTitles[i], drawerIcons.getDrawable(i)));
}
这是我在导航抽屉列表视图中使用的 DrawerItem class:
public class DrawerItem {
String title;
Drawable icon;
public DrawerItem(String title, Drawable icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public Drawable getIcon() {
return icon;
}
}
你应该改变你的 arralist (string)。使用 DrawerItems 的代码创建 ArrayList(如果你想要多个 textview 和 imageview,你可以向其中添加更多元素class。
我一直在尝试创建一个 ListView,并且我已经成功创建了一个。但是,我的 ListView 现在对列表中的每个项目使用相同的图标。当我一直在寻找答案时,人们只是展示了少于 10 个项目的列表示例。但是我的列表包含超过 100 个项目。
对于文本,我刚刚在 string.xml 中创建了一个字符串数组,并使用
检索了它们Resources res = getResources();
String[] list_of_names = res.getStringArray(R.array.names);
但是我如何对图标做同样的事情呢? Can/Should 我在单独的文件中定义它们,就像我对字符串数组所做的那样?我的图标在我的可绘制文件夹中。
此外,我是否应该为我的列表图标使用一组图像?还有别的办法吗?
感谢帮助!
编辑:
CustomAdapter.java
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] listNames) {
super(context, R.layout.list_item_copy, listNames);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.list_item_copy, parent, false);
String item = getItem(position);
TextView textView = (TextView) customView.findViewById(R.id.name);
ImageView imageView = (ImageView) customView.findViewById(R.id.icon);
textView.setText(item);
imageView.setImageResource(R.drawable.icon_red);
return customView;
}
}
MainActivity.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Resources res = getResources();
String[] listNames = res.getStringArray(R.array.names);
ArrayAdapter<String> adapter = new CustomAdapter(getActivity(), listNames);
ListView list = (ListView) rootView.findViewById(R.id.nameList);
list.setAdapter(adapter);
return rootView;
}
这会创建一个所有项目都具有相同图标的列表,但图标应该不同。
如果有什么不同,我已经遵循了新波士顿的教程:http://youtu.be/nOdSARCVYic?list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
你可以在strings.xml里面定义这样:
<array name="drawerIcons">
<item>@drawable/ic_action_design</item>
<item>@drawable/ic_action_develop</item>
</array>
您可以将它添加到您的 ArrayList 中:
ArrayList<DrawerItem> drawerItems = new ArrayList<>();
final String[] drawerTitles = getResources().getStringArray(R.array.drawer);
final TypedArray drawerIcons = getResources().obtainTypedArray(R.array.drawerIcons);
for (int i = 0; i < drawerTitles.length; i++) {
drawerItems.add(new DrawerItem(drawerTitles[i], drawerIcons.getDrawable(i)));
}
这是我在导航抽屉列表视图中使用的 DrawerItem class:
public class DrawerItem {
String title;
Drawable icon;
public DrawerItem(String title, Drawable icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public Drawable getIcon() {
return icon;
}
}
你应该改变你的 arralist (string)。使用 DrawerItems 的代码创建 ArrayList(如果你想要多个 textview 和 imageview,你可以向其中添加更多元素class。