当我按下我的viewPAger中的图像时打开活动或对话框

时间:2014-06-01 07:36:05

标签: android android-viewpager

在我的根活动中,我有由VIewPager显示的幻灯片放映。 在我的情况下,当我在幻灯片放映上按下图像时,我想打开新的活动或对话框或类似的东西。 通常我使用OnclickListener并为其分配图片ID,但没有得到任何反馈。

RootActivity.java

    public class RootActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_root);

        // Image slide show 
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        ImageAdapter.setTimer(viewPager, 3, 0);
         // End Image slide show

        ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
        vp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                System.out.print("OK ...");

            }
        });
}

}

ImageAdapter.java

public class ImageAdapter extends PagerAdapter {

    final static Handler handler = new Handler();
    public static Timer swipeTimer ;
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.ax1,
        R.drawable.ax2,
    };
    ImageAdapter(Context countDownTimer){
        this.context=countDownTimer;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);

    }


    public static void setTimer(final ViewPager myPager, int time, final int curPage){

        final Runnable Update = new Runnable() {
            int NUM_PAGES =5;
            int currentPage = curPage ;
            public void run() {
                if (currentPage == NUM_PAGES ) {
                    currentPage = 0;
                }
                myPager.setCurrentItem(currentPage++, true);
            }
        };

        swipeTimer = new Timer();
        swipeTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(Update);
            }
        }, 1000, time*1000);

}
}

2 个答案:

答案 0 :(得分:2)

直接将onClickListener(...)设置为ImageView中的ImageAdapter,如:

 @Override
public Object instantiateItem(ViewGroup container, int position) {
  ImageView imageView = new ImageView(context);
  int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
  imageView.setPadding(padding, padding, padding, padding);
  imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  imageView.setImageResource(GalImages[position]);

  imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

          //Do your job

        }
    });

  ((ViewPager) container).addView(imageView, 0);
  return imageView;
 }

答案 1 :(得分:2)

好的,首先要做的事情......

1。 RootActivity.onCreate()中的这一行没有多大意义:

ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);

您正尝试使用可绘制资源ID在布局中查找ViewPager对象。因此,请删除此行及onCreate()之后的所有内容。

2。您只能将OnClickListener附加到现有视图,因为PagerAdapters没有OnItemClickListener,您需要将OnClickListener附加到相应的视图中在适配器的instantiateItem()方法中。

我附上了代表这些要点的修改版代码。

RootActivity.java

public class RootActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_root);
        // Image slide show
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        ImageAdapter.setTimer(viewPager, 3, 0);
        // End Image slide show
    }
}

ImageAdapter.java

寻找 // Begin Added by SpaceLord 评论

public class ImageAdapter extends PagerAdapter {

    final static Handler handler = new Handler();
    public static Timer swipeTimer;
    Context context;
    private int[] GalImages = new int[]{
            R.drawable.one,
            R.drawable.two,
            R.drawable.three,
            R.drawable.ax1,
            R.drawable.ax2,
    };

    ImageAdapter(Context countDownTimer) {
        this.context = countDownTimer;
    }

    @Override
    public int getCount() {
        return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageResource(GalImages[position]);

        // Begin Added by SpaceLord
        imageView.setOnClickListener(itemClickListener);
        imageView.setTag(position);
        // End Added by SpaceLord

        container.addView(imageView, 0);
        return imageView;
    }

    // Begin Added by SpaceLord
    private final View.OnClickListener itemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int tag = (Integer) v.getTag();
            switch (tag) {
                // Do whatever you need to for each slide click here
                case 0:
                    Log.d(ImageAdapter.class.getSimpleName(), "Clicked on ONE");
                    break;
                case 1:
                    Log.d(ImageAdapter.class.getSimpleName(), "Clicked on TWO");
                    break;
                case 2:
                    Log.d(ImageAdapter.class.getSimpleName(), "Clicked on THREE");
                    break;
                case 3:
                    Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX1");
                    break;
                case 4:
                    Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX2");
                    break;
            }
        }
    };
    // End Added by SpaceLord

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);

    }


    public static void setTimer(final ViewPager myPager, int time, final int curPage) {

        final Runnable Update = new Runnable() {
            int NUM_PAGES = 5;
            int currentPage = curPage;

            public void run() {
                if (currentPage == NUM_PAGES) {
                    currentPage = 0;
                }
                myPager.setCurrentItem(currentPage++, true);
            }
        };

        swipeTimer = new Timer();
        swipeTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(Update);
            }
        }, 1000, time * 1000);

    }
}