问题描述
注意:解决了此特定问题,但存在严重的后续问题。看看
Note: This specific problem is solved, but there are serious follow-up problems. Have a look at GestureDetector - Detect double click in GridView item's although returning false in onTouchEvent()
我想检测 GridView $ c中不同项目的双击$ c>图像。
因此,我为中的每个项目 -
适配器的方法。 imageView
分配了一个单独的 OnTouchListener
getView () gestureDetector
是适配器类的成员变量。
I want to detect double clicks on distinct items in a GridView
of images.Therefore I assigned a separate OnTouchListener
to each item-imageView
in the getView()
method of the adapter. The gestureDetector
is a member variable of the adapter-class.
private GestureDetectorCompat gestureDetector;
public ImageGridViewAdapter(Context c, ArrayList<UriWrapper> startUpImages) {
mContext = c;
uriManager = new UriManager(startUpImages);
gestureDetector = new GestureDetectorCompat(mContext, new SingleTapConfirm());
}
public View getView(final int position, View recycled, ViewGroup parent) {
ViewHolder holder;
if (recycled == null) {
..... find items by id
} else{
holder = (ViewHolder) recycled.getTag();
}
// Set listener to item image
holder.image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Always returns false, the gestureDetector does not detect anything
boolean ret = gestureDetector.onTouchEvent(event);
// At least the onTouch-callback gets called with the correct position
Log.e(TAG, "onTouch returned " + ret + " at position " + position);
return true;
}
});
// Use glide library to load images into the image views
Glide.with(mContext)....into(holder.image);
return recycled;
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.e(TAG, "onSingleTapConfirmed"); // never called..
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e(TAG, "onDoubleTap"); // never called..
return super.onDoubleTap(e);
}
}
OnTouchListener
的工作,并获得正确的位置调用。
但是,不管我在做什么, GestureDetector
的方法都不会被调用。这段代码似乎有什么问题?
The OnTouchListener
's work and get called with the correct position.However, no matter what I am doing, the methods of the GestureDetector
are never called. What seems to be the issue with this code?
更新:onTouch回调需要返回true,至少现在至少GestureDetector有效。但是,返回true会打破其他功能,因为我的GridView有一个长时间点击选择模式和一个全局OnTouchListener。
Update: The onTouch-callback needs to return true, now at least the GestureDetector works. However, returning true breaks the rest of the functionality, since I have a long-click-selection-mode and a global OnTouchListener for my GridView.
第二次更新:
合并特定于项目的OnTouchListener和全局OnTouchListener无法正常工作。 (只在特定项目上识别滑动手势)
我希望能够通过创建自定义视图扩展ImageView并在其中分配特定于项目的OnTouchListener来解决这两个问题。
Second Update:Merging the item-specific OnTouchListener and the global OnTouchListener did not work properly. (swipe gestures only recognized on certain items)I hope that I can work around these two problems by creating a custom View extending ImageView and assigning the item-specific OnTouchListener there.
推荐答案
private GestureDetectorCompat gestureDetector;
// in your adapter constructor
gestureDetector = new GestureDetector(context, new SingleTapConfirm());
public View getView(final int position, View recycled, ViewGroup parent) {
ViewHolder holder;
if (recycled == null) {
.....find items by id
} else {
holder = (ViewHolder) recycled.getTag();
}
// Set listener to item image
holder.image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Use lazy initialization for the gestureDetector
gestureDetector.onTouchEvent(event);
// At least the onTouch-callback gets called with the correct position
return true;
}
});
// Use glide library to load images into the image views
Glide.with(mContext)....into(holder.image);
return recycled;
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.e(TAG, "onSingleTapConfirmed"); // never called..
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e(TAG, "onDoubleTap"); // never called..
return super.onDoubleTap(e);
}
}
更新:
@Override
public boolean onTouch(View v, MotionEvent event) {anything
gestureDetector.onTouchEvent(event);
return true;
}
这篇关于GestureDetector检测GridView项目上的DoubleClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!