Android的 - 滚动时ListView项风格失败 [英] Android - ListView item style fails when scrolling

查看:135
本文介绍了Android的 - 滚动时ListView项风格失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的的ListView 的问题。我使用它们了几个项目,并在他们每个人发生错误。

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.

当我滚动图像/加粗文本是不是在正确的位置,当我在某个时刻快速滚动起来,再次下调(万一的的)nearl所有项目大胆。

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?

谢谢!

推荐答案

现在的问题是,因为使用的是ViewHolder模式,在视图模式持有的android重用退出视图。因此,当它被重用previously生成的视图这是粗体。现在,如果当它试图渲染视图,但无法满足您应用的情况下,它只是呈现previous粗体字脸。所以下面可以解决你的问题:

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);
}

这篇关于Android的 - 滚动时ListView项风格失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆