我必须能够检测到按下和释放按钮,但我还必须在触摸的gridview中找到该项目的位置。我基本上有一个网格视图,它是一个d-pad控件。因此,当用户按下它时,我需要它发送“go”,当他们发布时我需要它发送“停止”
如下所示,没有办法告诉他们何时取下按钮(我知道!)。
Grid.setAdapter(new ImageAdapter(c, mThumbIds));
Grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Message msg = new Message();
msg.what = what;
msg.arg1 = position;
cHandler.sendMessage(msg);
}
});
但如果我使用ontouch列表器,我无法分辨他们选择的网格中的哪个项目
Grid.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Message msg = new Message();
msg.what = 255;
msg.arg1 = 255;
cHandler.sendMessage(msg);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Message msg = new Message();
msg.what = what;
msg.arg1 = 55;
cHandler.sendMessage(msg);
}
return false;
}
});
非常感谢您的时间!
///这就是我一直在尝试做的事情,我永远无法理解gridview的不同部分。 r.getX等总是返回0.如果我有资源的id我怎么能拉出它的矩形?
Grid.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Rect r = new Rect();
ImageView imageView = new ImageView(c);
for(int n: mThumbIds){
imageView.setImageResource(n);
r = imageView.getDrawable().getBounds();
if(r.contains((int)event.getX(),(int)event.getY()))
{
Message msg = new Message();
msg.what = what;
msg.arg1 = n;
msg.arg2 = (int)event.getRawY();
cHandler.sendMessage(msg);
break;
}else{
Message msg = new Message();
msg.what = what;
msg.arg1 = r.centerX();
msg.arg2 = n;
cHandler.sendMessage(msg);
}
}// end for
}// end if action up
答案 0 :(得分:0)
OR
你已经完成了!