In my activity I have an imageView(at the top) and textView(at the bottom). Using both views I display an image and its corresponding description. I have implemented fling using 'SimpleOnGestureListener'..so that user can fling through different images and its corresponding description(on every fling, I load different image and show it to user).
Now the problem started when I added onClickListener for the ImageView( I need to detect onClick so that I can display a Toast with description of the image). But the onClick seems to be blocking the fling event. How do I detect fling.
Have already gone through the following questions(and few others) in this forum..
android - giving onTouch priority over onClick
Android onClick blocking onFling
but still could not identify the solution.
Given below is the code
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);
}