我通过使用具有备用背景颜色的适配器
来创建自定义listview
。
问题是字符串文本(超过 100 多个字符串)被设置为 Color.BLACK 的颜色,但在 listview 中前几个字符串是白色然后其余的都是黑色的。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) super.getView(position, convertView, parent);
int colorPos = position % color.length;
tv.setTextColor(Color.BLACK);
view.setBackgroundColor(color[colorPos]);
return view;
}
我无法检测到为什么第一个是白色然后其他是黑色的问题,是否与加载有关或其他原因?因为我的文本(字符串)超过 100 个。
更新
这是 ListView 代码:
MyAdapter adapter = new MyAdapter(this,
android.R.layout.simple_list_item_1,
Strings.Advices);
setListAdapter(adapter);
还有字符串:
public class Strings {
public static String Advices[] = {
"advice",
"advice",
"adc",
"add",
};
}
最佳答案
您的 TextView 不执行任何操作。通常,您需要使用 findViewById()
来定位行布局中的 TextView。由于您使用的 android.R.layout.simple_list_item_1
它本身就是一个 TextView,因此我们可以跳过该步骤:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
int colorPos = position % color.length;
tv.setTextColor(Color.BLACK);
view.setBackgroundColor(color[colorPos]);
return view;
}
但是任何内置 Adapter 的 getView()
方法都非常通用,因此速度很慢...您应该编写自己的方法以充分利用 Adapter 的回收器和 ViewHolder 概念。请观看此 Google Talk Turbo Charge Your UI (和/或 World of ListView )帮助您编写高效的适配器。
The problem is that the text which are strings ( over 100+ of strings ) are set to the color of Color.BLACK but in the listview the first couple of strings are white and then the rest are black
我假设当您滚动 ListView 时会发生此更改,这直接与 View 回收器的行为方式相关。
关于Android ListView 文本颜色 白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14025839/