I am swiping my images using viewflipper class in android.I have image array. Now each image have their own functionality.For Example 1st image should take me to another activity and similarly others. What i want to do is to make click listener of each image so that i can perform different tasks according to the image. Here is my code:
public class gesture extends AppCompatActivity {
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector;
MediaPlayer mp;
int i;
int a=0;
ImageView imageView;
int[] image = {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6
};
int[] audio={
R.raw.p1,
R.raw.p2,
R.raw.p3,
R.raw.p4,
R.raw.p5,
R.raw.p6
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
CustomGestureDetector customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(this, customGestureDetector);
// Get the ViewFlipper
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
if (mp != null) {
mp.stop();
mp.release();
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();
// Add all the images to the ViewFlipper
for (i = 0; i < image.length; i++) {
imageView = new ImageView(this);
imageView.setImageResource(image[i]);
mViewFlipper.addView(imageView);
}
}
class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Swipe left (next)
if (e1.getX() > e2.getX()) {
if (mp != null) {
mp.stop();
mp.release();
}
a++;
if (a==6){
a=0;
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();
mViewFlipper.showNext();
}
// Swipe right (previous)
if (e1.getX() < e2.getX()) {
if (mp != null) {
mp.stop();
mp.release();
}
a--;
if(a==-1){
a=5;
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();
mViewFlipper.showPrevious();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return false;
}
return super.onOptionsItemSelected(item);
}
Thanks in advance.