开发者

Why wont my ImageButton change imageresource?

I'm trying update imagebuttons that I have in a gridview that exists in a compound component. I have tried several different methods but somehow I can't get i to work. Here is my code:

This is a class with a merged xml-layout:

public class DialPadView extends RelativeLayout {

private GridView gv;

public DialPadView(Context context, AttributeSet attr) {
    super(context, attr);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.dialpad, this, true);
    gv = (GridView)findViewById(R.id.gv_dials);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    TableLayout tl = (TableLayout)findViewById(R.id.tl_main);
    int leftHeight = this.getHeight() - tl.getHeight();
    gv.setAdapter(new DialpadAdapter(this.getContext(), leftHeight));
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

The DialpadAdapters getView method looks like this:

public View getView(int position, View convertView, ViewGroup parent) {
    DialButton imgBtn;

    if(convertView == null) {
        imgBtn = new DialButton(mContext);
        imgBtn.setLayoutParams(new GridView.LayoutParams(btnHeight, btnHeight));
        imgBtn.setScaleType(DialButton.ScaleType.FIT_CENTER);
        imgBtn.setPadding(2, 2, 2, 2);
    } else {
        imgBtn = (DialButton) convertView;
    }

    imgBtn.setImageResource(mDigitsId[position]);
    imgBtn.setBackgroundColor(color.transparent);


    imgBtn.setPosition(position);
    imgBtn.setHapticFeedback开发者_StackOverflowEnabled(true);
    imgBtn.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            DialButton ib = (DialButton) v;
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                ib.changeImage(mDigitsOverId[ib.getPosition()]);
                ib.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                Log.d("BUTTON PRESSED", "THIS BUTTON IS INDEED PRESSED!");
            } else {
                ib.changeImage(mDigitsId[ib.getPosition()]);
            }
            return false;
        }
    });

    return imgBtn;
}

And finally my DialButton is simply a extended ImageButton:

public class DialButton extends ImageButton {

private int _position;

public DialButton(Context ctx) {
    super(ctx);
}

public void changeImage(int resource_id) {
    this.invalidate();
    this.setImageResource(resource_id);
    this.invalidate();
}


public void setPosition(int pos) {
    _position = pos;
}

public int getPosition() {
    return _position;
}

}

The log-message in the getView method prints so I know that getView gets called but somehow the images doesn't change. Please can anyone see what it is that I'm doing wrong?

EDIT: To clarify, I have already tried passing a resource-id directly.


You can try to use notifyDataSetChanged() for your DialpadAdapter adapter. It should refresh attached grid or gallery view.


To start, I doubt you need to call this.invalidate() before setting the new image, only after.

Also, I would try doing this:

final DialButton imgBtn;

...

imgBtn.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN ||
           event.getAction() == MotionEvent.ACTION_MOVE) {
            imgBtn.changeImage(mDigitsOverId[ib.getPosition()]);
            imgBtn.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
            Log.d("BUTTON PRESSED", "THIS BUTTON IS INDEED PRESSED!");
        } else {
            imgBtn.changeImage(mDigitsId[ib.getPosition()]);
        }
        return false;
    }
});

Because touch screens are sensitive and your finger is always moving at least a little bit, ACTION_MOVING tends to be the one that is getting called constantly during touch input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜