我正在开发一个使用 View-Flipper 的应用程序。在这个 View-Flipper 中,我有一个图像视图,它在自动模式下翻转图像。现在我想对视图 Flipper 中的每个翻转图像执行单击侦听器。所以我可以在哪里设置单击侦听器事件。
public class MainActivity extends Activity
{
@SuppressWarnings("unused")
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector;
int[] resources = {
R.drawable.t1,
R.drawable.t2,
R.drawable.t3,
R.drawable.t4,
R.drawable.t5
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewFlipper
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
//Automatically Start Flipping
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(5000); // for 5 sec
// Add all the images to the ViewFlipper
for (int i = 0; i < resources.length; i++)
{
ImageView imageView = new ImageView(this);
imageView.setPadding(40, 80, 40, 80);
imageView.setImageResource(resources[i]);
mViewFlipper.addView(imageView);
// Set in/out flipping animations
mViewFlipper.setInAnimation(this, android.R.anim.fade_in);
mViewFlipper.setOutAnimation(this, android.R.anim.fade_out);
OnGestureListener customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(MainActivity.this, customGestureDetector);
}}
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())
{
mViewFlipper.setInAnimation(MainActivity.this, R.anim.left_in);
mViewFlipper.setOutAnimation(MainActivity.this, R.anim.left_out);
mViewFlipper.showNext();
}
// Swipe right (previous)
if (e1.getX() < e2.getX())
{
mViewFlipper.setInAnimation(MainActivity.this, R.anim.right_in);
mViewFlipper.setOutAnimation(MainActivity.this, R.anim.right_out);
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.main, menu);
return true;
}
@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 true;
}
return super.onOptionsItemSelected(item);
}
}