Skip to content Skip to sidebar Skip to footer

Android - Listview Item Style Fails When Scrolling

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

Solution 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 boldnessif(condition) {
      holder.item_one.setTypeface(null, Typeface.BOLD);
}

Solution 2:

  1. For the Image thing, use an ImageLoader. This is what I use, and is very easy to customize.

  2. 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.

Solution 3:

if you have to do bold in some predefined specific position then do something like the following,

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderholder=null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.my_row_layout, null);
            holder = newViewHolder();
            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();
        }

        ObjectobjectItem= 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.

Post a Comment for "Android - Listview Item Style Fails When Scrolling"