最近遇到了这个问题并在寻求帮助时遇到了这篇文章,我想从我所做的事情中添加两件似乎有效的事情:
1)我将 onTouchListener 添加到适配器中的对象而不是活动或网格视图。
2) 在 OnTouchListener 中,我查找了 MotionEvent.ACTION_DOWN(第一次手指触摸)和 MotionEvent.ACTION_POINTER_DOWN(随后的手指触摸),这样我就可以获得多点触摸并立即处理它们,而无需等待用户抬起手指。
请注意,我将其命名为 ImageAdapter,即使我已经为每个都添加了一个 TextView,因为这样我可以将 TextView 背景用于图像,但将不可见的文本添加到 TextView 以便它与 Talkback 一起使用):
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return numCols * numRows;
}
public Object getItem(int position) {
return this;
}
public long getItemId(int position) {
return position;
}
// create a new TextView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) convertView;
}
// place any other initial setup needed for the TextView here
// here's our onTouchListener
textView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
boolean returnValue;
int thePosition = v.getId();
// MotionEvent.ACTION_DOWN gets the first touch
// MotionEvent.ACTION_POINTER_DOWN gets any subsequent touches (if you place a second finger on the screen)
// Between these I can get touches as soon as they happen, including multitouch support, without needing to wait until the user lifts their finger.
if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) {
TextView textView;
if (v == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) v;
}
// Do any processing based on the touch - I call a function and pass the position (number of cell, 1..n) and textview so can make changes to it as needed
ScreenTapped(thePosition, textView);
// I used a returnValue
returnValue = true;
} else returnValue = false;
return returnValue;
});
return textView;
} // getView
} //imageadapter