Image使用android中的Viewflipper类单击侦听器

时间:2017-09-09 10:00:12

标签: android onclicklistener android-viewflipper

我使用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);
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

首先在您的活动中实施OnClickListener

public class gesture extends AppCompatActivity implements View.OnClickListener {

然后为每个图像提供一个id

for (i = 0; i < image.length; i++) {
    imageView = new ImageView(this);

    imageView.setImageResource(image[i]);
    imageView.setId(i); // <---- assigning id
    mViewFlipper.addView(imageView);

}

最后,处理点击次数:

@Override
public void onClick(View v) {
   Class<?> c = null;
   int imageId = v.getId();

   try {
       c = Class.forName("ActivityToStartOn"+imageId);
   } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   Intent intent = new Intent(gesture.this, c);
   startActivity(intent);

}

注意:我假设您要开始的活动名为ActivityToStartOn0ActivityToStartOn1,....

答案 1 :(得分:1)

回答这个问题太晚了,但这可能会帮助viewFlipper.getChildAt(i).setOnClickListener()为我工作的人:

下面是我的工作代码示例:

            for (int i = 0; i < room.getImages().size(); i++) {
                ImageView image = new ImageView(RoomDetailActivity.this);
                image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Picasso.get().load(room.getImages().get(i)).into(image);
                viewFlipper.addView(image);
                final int temp = i;
                viewFlipper.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(RoomDetailActivity.this,room.getImages().get(temp), Toast.LENGTH_SHORT).show();
//                        Intent intent = new Intent(RoomDetailActivity.this,PhotoZoomViewActivity.class);
//                        intent.putExtra("url",room.getImages().get(temp));
//                        startActivity(intent);
                    }
                });
            }