我已经在我的视图翻转器中动态创建了imageview,它运行得很流畅。但是,当我想点击图像时,我需要放大图像并对其进行处理。如何获取ID。我需要能够区分已单击的图像。
提前谢谢。

    public class Home_Fragment extends Fragment{

.....

   private ViewFlipper vf;

    int gallery_grid_Images[]={R.drawable.a,R.drawable.b,R.drawable.c,
            R.drawable.d};

  private void setFlipperImage(int res) {
        //Log.i("Set Filpper Called", res+"");
        ImageView image = new ImageView(getActivity());



        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        image.setLayoutParams(lp);

        image.setAdjustViewBounds (true);
        image.setScaleType(ScaleType.CENTER_INSIDE);
        image.setClickable(true);

        image.setBackgroundResource(res);

        image.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id=(Integer) v.getTag();     //---> This will crash if i click the image
                //Toast.makeText(getActivity(), id+"", Toast.LENGTH_LONG).show();

            }
        });


        vf.setTag(res);

        vf.addView(image);
    }

最佳答案

下面的代码我已经用于滑动图像的viewflipper。您可以在forloop中添加图像的onclick事件。
幻灯片放映活动.java

package com.example.viewpagerexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class SlideShowActivity extends Activity {

private ViewFlipper myViewFlipper;
private float initialXPoint;
int[] image = { R.drawable.one_full, R.drawable.two_full,
    R.drawable.three_full, R.drawable.four_full, R.drawable.five_full,
    R.drawable.six_full, R.drawable.seven_full, R.drawable.eight_full,
    R.drawable.nine_full, R.drawable.ten_full };

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.slide_show);
myViewFlipper = (ViewFlipper) findViewById(R.id.myflipper);

for (int i = 0; i < image.length; i++) {
    ImageView imageView = new ImageView(SlideShowActivity.this);
    imageView.setImageResource(image[i]);
    myViewFlipper.addView(imageView);
    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
       // your code
   }});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
    initialXPoint = event.getX();
    break;
case MotionEvent.ACTION_UP:
    float finalx = event.getX();
    if (initialXPoint > finalx) {
        if (myViewFlipper.getDisplayedChild() == image.length)
            break;
        myViewFlipper.showNext();
    } else {
        if (myViewFlipper.getDisplayedChild() == 0)
            break;
        myViewFlipper.showPrevious();
    }
    break;
}
return false;
}
}

这里的图片只在用户刷卡时改变。
如果要以一定间隔自动刷卡,请添加以下代码。
myViewFlipper.setAutoStart(true);
myViewFlipper.setFlipInterval(3000);
myViewFlipper.startFlipping();

10-04 16:58