自定义ArrayAdapter< Object> ViewHolder:imageView中的几个项目的更改不仅仅是一个 [英] custom ArrayAdapter<Object> ViewHolder : imageView changes for several items in Listview not for one only

查看:195
本文介绍了自定义ArrayAdapter< Object> ViewHolder:imageView中的几个项目的更改不仅仅是一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我遇到这个自定义ArrayAdapter的问题

ok i encounter a problem with this custom ArrayAdapter

public class AuthorsAdapter extends ArrayAdapter<Entity> {

    View row;
    static class ViewHolder {
        TextView textViewTitle;
        TextView textViewEntitySummary;
        ImageView imageViewPicture;
    }
    public AuthorsAdapter(Context context, LinkedList<Entity> entity) {
        super(context, 0, entity);
    }
    @Override
    public View getView( final int position,  View convertView,  final ViewGroup parent) {
        row = convertView;
        final ViewHolder holder;

        if (row == null) {
            row = LayoutInflater.from(getContext()).inflate(R.layout.listview_search_author_template, parent, false);
            holder = new ViewHolder();
            holder.textViewTitle = (TextView) row.findViewById(R.id.textView_TitleTopLeft);
            holder.textViewEntitySummary = (TextView) row.findViewById(R.id.textView_EntitySummary);
            holder.imageViewPicture = (ImageView) row.findViewById(R.id.imageView_author);
            row.setTag(holder);
        } else
            holder = (ViewHolder) row.getTag();


        holder.textViewTitle.setText(getItem(position).getTitle());
        if (!getItem(position).getPictureURL().isEmpty())
            Picasso.with(getContext()).load(getItem(position).getPictureURL()).into(holder.imageViewPicture);
        holder.textViewEntitySummary.setText(getItem(position).getBiography());


        holder.imageViewPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getItem(position).isImageResized()) {
                    holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    getItem(position).setIsImageResized(true);
                } else if (getItem(position).isImageResized()) {
                    holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    getItem(position).setIsImageResized(false);

                } else {
                    holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));

                }

            }
        });


        return row;
    }

一切运作良好,期待imageView onclicklistener:

everything works well expected the imageView onclicklistener :

holder.imageViewPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!getItem(position).isImageResized()) {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
                        getItem(position).setIsImageResized(true);
                    } else if (getItem(position).isImageResized()) {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));
                        getItem(position).setIsImageResized(false);

                    } else {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));

                    }

                }
            });


}

我希望图像改变它的大小点击它,它工作,但当我在我的视图中向下滚动时,我的listView中的随机其他项目受到点击第一项的影响...当我进入我的列表视图时,图像不再调整大小(在我点击的索引处) )任何提示使这个工作完美无缺?

i want that the image change it size on click, it work but when i scroll down in my view, random other items in my listView get affected by clicking on first item... and when i come up in my listview the image is not resized anymore (at index where i clicked) any hints to make this working flawlessly?

推荐答案

好的我通过引用

private  LinkedList<Entity> entity;
public AuthorsAdapter(Context context, LinkedList<Entity> entity) {
    super(context, 0, entity);
    this.entity = entity;
}

在构造函数
然后而不是像你说的那样:

in the constructor then instead of doing like you said :

....  else{
              holder = (ViewHolder) convertView.getTag();
            }
                     if (!getItem(position).isImageResized()) {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
                        getItem(position).setIsImageResized(true);
                    } else if (getItem(position).isImageResized()) {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));
                        getItem(position).setIsImageResized(false);

                    } else {
                        holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));

                    } .....

我做了一个增强的for循环这个:

ive made an enhanced for loop like this :

....  else{
                  holder = (ViewHolder) convertView.getTag();
                }

for (Entity foo : entity) {
            if (!foo.isImageResized()) {
                holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                        25,
                        25));
            } else if (foo.isImageResized())
                holder.imageViewPicture.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));

        } ....

请注意我在我做了一些更改xml布局所以现在图像是正确的在标题:)下我让它匹配父级以获得更大尺寸的图像...

note that i do some changes in my xml layout so now the image is right Under the title :) and i make it match parent to have bigger sized image...

btw非常感谢提示@ Ammar aly它真的帮了我

btw thanks a lot for the hint @Ammar aly it really helped me

这篇关于自定义ArrayAdapter&lt; Object&gt; ViewHolder:imageView中的几个项目的更改不仅仅是一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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