在我的活动中,我有一个imageView(在顶部)和textView(在底部)。使用两个视图我显示图像及其相应的描述。我已经使用'SimpleOnGestureListener'实现了fling,以便用户可以浏览不同的图像及其相应的描述(在每次投掷时,我加载不同的图像并将其显示给用户)。
现在,当我为ImageView添加onClickListener时问题就开始了(我需要检测onClick,以便我可以显示带有图像描述的Toast)。但onClick似乎阻止了这一事件。我如何检测甩尾。
在这个论坛中已经完成了以下问题(以及其他一些问题)..
android - giving onTouch priority over onClick
Android onClick blocking onFling
但仍然无法确定解决方案。
以下是代码
public class MainActivity extends Activity{
ImageView m_imageView;
String[] m_imageNames = { "img1__", "img2__", "img2__"};
int m_index = 0;
Resources m_res;
Drawable m_drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("INFO", "Inside oncreate");
m_imageView = (ImageView)findViewById(R.id.imageView1);
m_res = getResources();
int imageId = m_res.getIdentifier(m_imageNames[m_index], "drawable", getPackageName());
Drawable drawable = m_res.getDrawable(imageId);
m_imageView.setImageDrawable(drawable);
m_imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("INFO", "Inside onClick");
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("INFO", "onTouchEvent detected");
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener
= new SimpleOnGestureListener(){
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//Fling code goes here
}
};
GestureDetector gestureDetector = new GestureDetector(null, SimpleOnGestureListener);
}
public class MainActivity extends Activity{
ImageView m_imageView;
String[] m_imageNames = { "img1__", "img2__", "img2__"};
int m_index = 0;
Resources m_res;
Drawable m_drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("INFO", "Inside oncreate");
m_imageView = (ImageView)findViewById(R.id.imageView1);
m_res = getResources();
int imageId = m_res.getIdentifier(m_imageNames[m_index], "drawable", getPackageName());
Drawable drawable = m_res.getDrawable(imageId);
m_imageView.setImageDrawable(drawable);
m_imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("INFO", "Inside onClick");
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("INFO", "onTouchEvent detected");
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener
= new SimpleOnGestureListener(){
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//Fling code goes here
}
};
GestureDetector gestureDetector = new GestureDetector(null, SimpleOnGestureListener);
}
答案 0 :(得分:0)
这些方法通常有boolean
个返回值,表示此事件已被使用,不应再传播。尝试更改返回值。