问题
I have an issue with my ListView
. I use them in several projects and the error occurs in every one of them.
In one case every item has a unique image, in the other case some items contain bold text, some don't.
When I scroll the images/bold texts are not in the right position and when I scroll quickly up and down again at some point (in case b) nearl all items are bold.
The only post I could find was this one but it didn't solve the issue.
Here's my custom adapter:
private class MyAdapter extends BaseAdapter {
private List<Object> obj;
private LayoutInflater inflater;
public MyAdapter(Context context, List<Object> obj_list) {
this.obj_list = obj_list;
this.inflater = LayoutInflater.from(context);
}
public int getCount(){
return this.obj_list.size();
}
public Object getItem(int position){
return this.obj_list.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.my_row_layout, null);
holder = new ViewHolder();
holder.item_one = (TextView)convertView.findViewById(R.id.txtItemOne);
holder.item_two = (TextView)convertView.findViewById(R.id.txtItemTwo);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Object objectItem = getItem(position);
//here I set the boldness
if(condition) {
holder.item_one.setTypeface(null, Typeface.BOLD);
}
holder.item_one.setText(objectItem .getItemOne());
holder.item_two.setText(objectItem .getItemTwo());
return convertView;
}
class ViewHolder {
TextView hItemOne;
TextView hItemTwo;
}
}
Has anyone an idea what I might be doing wrong? Or at least experienced the same issue?
Thanks!
回答1:
The problem is because you are using ViewHolder pattern, In the view holder pattern android reusing the exiting view. So when it is reusing previously generated view which was in bold face. Now if when it tries to render the view but fail to satisfy the condition you have applied , it just render the previous bold type face. So the following could solve your problem:
holder.item_one.setTypeface(null, Typeface.NORMAL);
//here I set the boldness
if(condition) {
holder.item_one.setTypeface(null, Typeface.BOLD);
}
回答2:
For the Image thing, use an ImageLoader. This is what I use, and is very easy to customize.
If a TextView should be bold, set it to bold. If it needs to be normal, set it to normal. ListViews recycle views, so you need to set the preference manually for each.
回答3:
if you have to do bold in some predefined specific position then do something like the following,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.my_row_layout, null);
holder = new ViewHolder();
holder.item_one = (TextView)convertView.findViewById(R.id.txtItemOne);
holder.item_two = (TextView)convertView.findViewById(R.id.txtItemTwo);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Object objectItem = getItem(position);
if(position==2 || position==5) {
holder.item_one.setTypeface(null, Typeface.BOLD);
holder.item_one.setText(objectItem .getItemOne());
}
else
holder.item_two.setText(objectItem .getItemTwo());
return convertView;
}
But remember if you remove the "else" then it will not work.
来源:https://stackoverflow.com/questions/17940604/android-listview-item-style-fails-when-scrolling