Android GridView第一个按钮不起作用 [英] Android GridView first button not working

查看:59
本文介绍了Android GridView第一个按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Android GridView时遇到了奇怪的问题.我创建了一个3x4的网格并将按钮插入到该网格中.我希望用户单击该按钮时按钮的背景发生变化.对于第一个按钮(索引为0的按钮-左上角)以外的所有按钮,此方法都可以正常工作.无论我做什么,OnClick事件侦听器都不会为该按钮触发.

I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.

这是我创建视图的代码:

Here is the code where I create the view:

public View getView(int position, View convertView, ViewGroup parent) {
    Button imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        Log.w("NOVO", "narejena nova celica");
        imageView = new Button(mContext);

        imageView.setPadding(8, 8, 8, 8);
    } else {
        Log.w("STARO", "stara celica");
        imageView = (Button) convertView;
    }

    imageView.setEnabled(true);

    int visina = parent.getHeight();
    int sirina = parent.getWidth();

    float dip = mContext.getResources().getDisplayMetrics().density;
    float margin = 10*dip;

    int view_height = (int)(visina - 3*margin)/4;
    int view_width = (int)(sirina - 2*margin)/3;

    int view_dim = 0;
    if (view_height <= view_width)
        view_dim = view_height;
    else
        view_dim = view_width;

    imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));

    imageView.setId(position);
    imageView.setOnClickListener(celice.get(position));
    /*imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
            //toast.show();
            celice.get(v.getId()).celicaVisible(4000);
        }});*/

    celice.get(position).id = position;
    celice.get(position).setButton(imageView);

    return imageView;
}

如果我替换

imageView = (Button) convertView;

使用

imageView = new Button(mContext);

然后触发onClick(),但背景仍然没有改变.所有其他按钮均按预期工作.

then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.

这是定制类"Celica",它负责实际工作-更改背景...

And here is the custom class "Celica" that takes care of the actual work - changing the background...

public class Celica implements OnClickListener {

public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;

Celica(Drawable s1, Drawable s2) {
    this.slikca1 = s1;
    this.slikca2 = s2;
}

void celicaVisible(int millis) {
    if (odkrit)
        return;

    Log.w("TEST", "prizganih " + buttonsVisible);
    if (buttonsVisible >= 2)
        return;

    odkrit = true;
    buttonsVisible++;
    tim = new Timer();
    tim.schedule(new timerDone(), millis);

    ((Activity)con).runOnUiThread(new Runnable() {
           @Override
           public void run() {
               but.setBackground(slikca2);
           }
    });

}

void setButton(Button b) {
    but = b;

    ((Activity)con).runOnUiThread(new Runnable() {
        @Override
        public void run() {
           but.setBackground(slikca1);
        }
 });
}

class timerDone extends TimerTask {

    @Override
    public void run() {
        if (!najden) {
            odkrit = false;

            ((Activity)con).runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       but.setBackground(slikca1);
                   }
            });
        }

        buttonsVisible--;
        tim.cancel();
    }

}

@Override
public void onClick(View v) {
    celicaVisible(4000);
}

}

推荐答案

我实际上已经弄清楚了.如果我使用onClick()方法提供的视图,而不是在创建Celica对象时保存实际按钮,则一切正常.

I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.

所以基本上添加:

but = (Button) v;

使用onClick()方法解决了该问题.

to the onClick() method solved the problem.

这篇关于Android GridView第一个按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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