我正在使用android中的viewflipper类滑动我的图像。我有图像数组。现在每个图像都有自己的功能。例如,第一个图像应该带我去另一个活动和类似的其他活动。我想要做的是让每个图像的点击监听器,以便我可以根据图像执行不同的任务。这是我的代码:
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);
}
提前致谢。