在列表视图的onClick改变图像的Andr​​oid ImageView的工作不正常 [英] Android Imageview in Listview onClick change image doesn't work properly

查看:130
本文介绍了在列表视图的onClick改变图像的Andr​​oid ImageView的工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,并在每一个ListViewItem的有一个小星星的ImageView的(对标记为最爱)。因此,我把一个OnClickListener到ImageView的每个项目在定制ArrayAdapter。

  imgStar.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            位图的位图=((BitmapDrawable)imgStar.getDrawable())getBitmap();
            位图bitmap2 = ((BitmapDrawable)(context.getResources().getDrawable(R.drawable.ic_action_not_important))).getBitmap();            如果(位图!= bitmap2){
                    imgStar.setImageResource(R.drawable.ic_action_not_important);
            }其他{
                    imgStar.setImageResource(R.drawable.ic_action_important);
            }
        }
    });

问题:当我得到一些项目,然后点击例如在第一项的明星,形象的变化,因为它应该,但一些项目降低图像的变化太o.O
不会进入我的头是它只是改变(下面的其他项目),code,将在onclick只对执行要执行的图像事情:我用一些code测试它该项目我真的不点击的之一,形象也会发生变化。

为什么列表中的变化也随机其他项目的形象?我希望有人能帮助我。

自定义适配器Constructore code

 公共LinkArrayAdapter(上下文CON,诠释textViewResourceId){
    超(CON,textViewResourceId);
    上下文= CON;}


解决方案

主要的问题是,你不能更改的onClick物品的图片然后离开它并希望它会在名单上的每一个项目进行更新。因为的onClick 被调用在不同的时间比 getView 。所以,你必须设置的的onClick 之外,但在 getView 所以每次的 getView项目图片要求,将设置相应的图像用于该项目的特定项目。

在您的CustomAdapter类中定义一个布尔值数组:

 私人布尔[]星;

然后在你的类的构造方法,初始化为:

  this.stars =新的布尔[items.size()];

的onClick 方法:

  // **编辑在点击应用图像更新**
!星[位置] =明星[位置]
notifyDataSetInvalidated();

最后在自定义适配器的 getView()方法
(确保这一点code的不是其他任何内部块):

 如果(星[位置])
  imgStar.setImageResource(R.drawable.ic_action_important);
其他
  imgStar.setImageResource(R.drawable.ic_action_not_important);

I have a ListView and in every single ListviewItem there is an ImageView with a little star (for marking it as favourite). Therefore I put an OnClickListener to the ImageView on every item in the custom ArrayAdapter.

imgStar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = ((BitmapDrawable)imgStar.getDrawable()).getBitmap();
            Bitmap bitmap2 = ((BitmapDrawable)(context.getResources().getDrawable(R.drawable.ic_action_not_important))).getBitmap();

            if(bitmap != bitmap2) {
                    imgStar.setImageResource(R.drawable.ic_action_not_important);
            } else {
                    imgStar.setImageResource(R.drawable.ic_action_important);
            }
        }
    });

The problem: When I get some items and click for example on the star of the first item, the image changes as it should but a few items lower the image changes too o.O I tested it with some code: The thing that won't get into my head is it is only changing the image (on the other item below), code that would be executed in the onclick is only executed for the item I really click not for the one where the image changes too.

Why does the image of a random other item in the list change also? I hope someone can help me.

Custom Adapter Constructore Code

public LinkArrayAdapter(Context con, int textViewResourceId) {
    super(con, textViewResourceId);
    context = con;

}

解决方案

The main problem is that you can't change the image of items in the onClick then leave it and hope it will be updated on every item on the list. Because onClick get called in different time than getView. So you must set item images outside of onClick but in the getView so every time that getView called for a specific item it will set the appropriate image for that item.

Define a boolean array in your CustomAdapter class as:

private boolean[] stars;

Then in constructor method of your class, initialize it as:

this.stars = new boolean[items.size()];

In the onClick method:

// **Edited to apply image update at click**
stars[position] = !stars[position];
notifyDataSetInvalidated();

At last in the getView() method of custom adapter (ensure this code is not in any other inner blocks):

if (stars[position])
  imgStar.setImageResource(R.drawable.ic_action_important);
else
  imgStar.setImageResource(R.drawable.ic_action_not_important);

这篇关于在列表视图的onClick改变图像的Andr​​oid ImageView的工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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