简体   繁体   中英

How can I add OnClickListener to PagerAdapter

I'm using PagerAdaper for image slide in my app. I want to add listener on it mean if the image show when i click on it it say something. let suppose i have an image of apple now i want to add listener on it when i click on it it say apple. i want to add the same on all raw files.

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);
    }

}

There are several different possible solutions. One way to connect each view with its image resource using a 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);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM