从CustomAdapter的getView()意外行为 [英] Unexpected behaviour from getView() of CustomAdapter

查看:153
本文介绍了从CustomAdapter的getView()意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫有一个 ViewHolder 这样的类

    static class ViewHolder {
    protected String fileName;
    protected Bitmap bitmap = null;
    protected CheckBox checkBox;
    protected int position;
    protected int resourceId = 0;
    protected ImageView imageView;
    protected TextView textView;
}

和在我的 getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if(convertView != null)
    {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if(!holder.fileName.equals(fileList.get(position)))
            convertView = null;
    }
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.image_layout, null, false);
        viewHolder = new ViewHolder();

        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.text);

        // Set viewHolder attributes
        viewHolder.position = position;
        viewHolder.fileName = fileList.get(position);;

        // set the checkbox
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

        // Set the path of the file
        final String filePath = context.getBasePath(position);

        if(new File(filePath).isDirectory())
        {
            viewHolder.imageView.setImageResource(R.drawable.folder);
            viewHolder.resourceId = R.drawable.folder;
        }
        else
        {
            String mimeType = Utility.getMimeType(filePath);
            if(mimeType.contains("image"))
            {
                loadImage(viewHolder, viewHolder.imageView, filePath);
            }
            else
                viewHolder.resourceId = handleFile(viewHolder.imageView, filePath);
        }
        convertView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder) convertView.getTag();

        if(viewHolder.bitmap == null)
            viewHolder.imageView.setImageResource(viewHolder.resourceId);
        else
            viewHolder.imageView.setImageBitmap(viewHolder.bitmap);
    }

    viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
            viewHolder.checkBox.setChecked(checked);
            listener.onFileStateChanged(viewHolder.position, checked);
        }
    });

    // set the fileName
    viewHolder.textView.setText(viewHolder.fileName);

    if(checkBoxVisibility)
        viewHolder.checkBox.setVisibility(View.VISIBLE);
    else
        viewHolder.checkBox.setVisibility(View.INVISIBLE);

    return convertView;
}

而在这个时候,当我刷新使用适配器 notifydatasetChanged()不改变数据,在 ViewHolder 的我得到通过调用 getTag()返航错了标签。这是工作像

And at this time the when i refresh the Adapter using notifydatasetChanged() without changing data, the ViewHolder that i get by calling getTag() was returning wrong tags. It was working like

如果该位置为1持有人的对象是认为在位置0即;在previous对象返回。

If the position is 1 the holder object was of the view at position 0. ie; the previous object was returned..

Atlast我发现了一个<一个href="http://stackoverflow.com/questions/11186004/yet-another-getview-called-multiple-times">post

Atlast I found a post

这是不一样的,但用同样的方法和解决方案,为我工作。我不知道该怎么..

which is not the same but with the same method and the solution works for me .. I don't know how ..

将溶液

的ListView 的宽度设置为 MATCH_PARENT 在XML中,在后是身高

setting the width of ListView to MATCH_PARENT in xml, In the post it was height.

有没有人知道,什么是对这种行为的原因是什么?没有逻辑,我可以找到这背后的问题

Does any one know , What is the reason for such a behaviour ? There is no logic that i can find behind this issue

推荐答案

看来,当你不使用此解决方案,Android已经来调整ListView的布局几次,直到它完成。这就是为什么有很多用户抱怨 getView()被称为比它更次应该。

Seems that when you don't use this workaround, Android has to resize the listview's layout several times until it's performed. That's why there's many users complaining about getView() being called more times than it "should".

总之,要意识到 getView()不叫secuentially,这就是所谓的顺序Android的决定,所以不要指望是为了显示标签。

Anyway, be conscious that getView() is not called secuentially, it's called in the order that Android determines, so don't expect to be tags shown in order.

---编辑---

这里来确认,第48页。希望这有助于!

And here comes the confirmation, page 48. Hope this helps!

这篇关于从CustomAdapter的getView()意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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