在ListView中滚动会丢失文本颜色和字体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ListView中滚动会丢失文本颜色和字体相关的知识,希望对你有一定的参考价值。
我不确定它为什么不起作用,但我的代码应该做的很简单。
我有一个ListView,其中包含我在getView()中声明的默认文本颜色(BLACK)和字体(MONOSPACE),因此在创建视图时一切正常。
当我从列表中选择一个项目时,它应该将文本颜色更改为RED并将字体更改为MONOSPACE和BOLD,如果我单击另一个项目,它会将该项目更改为RED和BOLD并将之前的文本返回到BLACK和NORMAL。这很好用,但如果我滚动远离RED和BOLD文本直到它不可见,然后向后滚动,文本不再是RED和BOLD,而是BLACK和NORMAL。
非常感谢帮助!谢谢
private ArrayList<String> list;
private ListView myView;
private ListAdapter listAdapter;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("Hello world " + i);
}
listAdapter = new ListAdapter();
myView = (ListView) findViewById(R.id.myList);
myView.setAdapter(listAdapter);
myView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setTextColor(Color.RED);
tv.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
}
});
}
public class ListAdapter extends BaseAdapter {
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.listview_layout, null);
TextView txt = (TextView) view.findViewById(R.id.textView);
txt.setText(list.get(i));
txt.setTextColor(Color.BLACK);
txt.setTypeface(Typeface.MONOSPACE, Typeface.NORMAL);
return view;
}
}
答案
好的,首先你要通过标志保持行之间的样式差异。因此,不应将String列表传递给适配器,而应考虑包含字符串和标志对的列表。出现此问题的原因是ListView包含有限的视图数,并且在滚动发生时,它会为它们分配值。
public class MainActivity extends AppCompatActivity {
private List<Pair> list;
private ListAdapter listAdapter;
private ListView myView;
private int lastClickedPosition = 0;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add(new Pair("Hello world " + i, false));
}
listAdapter = new ListAdapter();
myView = (ListView) findViewById(R.id.myList);
myView.setAdapter(listAdapter);
myView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
list.get(lastClickedPosition).flag = false;
list.get(position).flag = true;
lastClickedPosition = position;
listAdapter.notifyDataSetChanged();
}
});
}
public class ListAdapter extends BaseAdapter {
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.listview_layout, null);
TextView txt = (TextView) view.findViewById(R.id.textView);
txt.setText(list.get(i).text);
if (list.get(i).flag) {
txt.setTextColor(Color.RED);
txt.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
} else {
txt.setTextColor(Color.BLACK);
txt.setTypeface(Typeface.MONOSPACE, Typeface.NORMAL);
}
return view;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
}
static class Pair {
String text;
boolean flag;
Pair(String text, boolean flag){
this.text = text;
this.flag = flag;
}
}
}
以上是关于在ListView中滚动会丢失文本颜色和字体的主要内容,如果未能解决你的问题,请参考以下文章