如何将OnClickListener添加到PagerAdapter

时间:2019-04-04 19:06:39

标签: java android android-viewpager android-pageradapter androidimageslider

我在我的应用程序中使用PagerAdaper制作图像幻灯片。我想在其上添加侦听器,这意味着如果单击图像时显示该图像,则表示某些内容。假设我现在有一个苹果的图像,我想在单击它时添加监听器,说苹果。我想在所有原始文件上添加相同的内容。

package com.example.slider;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    private Context mcontext;
    private int[] myImages = new int[] {R.raw.one , R.raw.two, R.raw.three,
            R.raw.four , R.raw.five , R.raw.six, R.raw.seven, R.raw.eight,
            R.raw.nine, R.raw.ten, R.raw.eleven, R.raw.twele , R.raw.thirteen, R.raw.fourteen,
            R.raw.fifteen, R.raw.one_image, R.raw.two_image,
            R.raw.three_image, R.raw.four_image, R.raw.five_image,
            R.raw.six_image, R.raw.seven_image};

    ImageAdapter(Context context){
        mcontext = context;
    }


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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imageView = new ImageView(mcontext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(myImages[position]);
        container.addView(imageView,0);
        return imageView;
    }

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

}

1 个答案:

答案 0 :(得分:0)

有几种不同的可能解决方案。一种使用HashMap连接每个视图与其图像资源的方法。

public class ImageAdapter extends PagerAdapter {
private Context mcontext;
private int[] myImages = new int[] {R.raw.one , R.raw.two, R.raw.three,
        R.raw.four , R.raw.five , R.raw.six, R.raw.seven, R.raw.eight,
        R.raw.nine, R.raw.ten, R.raw.eleven, R.raw.twele , R.raw.thirteen, R.raw.fourteen,
        R.raw.fifteen, R.raw.one_image, R.raw.two_image,
        R.raw.three_image, R.raw.four_image, R.raw.five_image,
        R.raw.six_image, R.raw.seven_image};

ImageAdapter(Context context){
    mcontext = context;
}

private HashMap<View, Integer> viewToImageResource = new HashMap<>();
private View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Integer clickedImageResource = viewToImageResource.get(v);
        // Do something with clickedImageResource
    }
};


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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {

    ImageView imageView = new ImageView(mcontext);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageResource(myImages[position]);
    container.addView(imageView,0);
    viewToImageResource.put(container, myImages[position]);
    container.setOnClickListener(clickListener);
    return imageView;
}

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

}