onClick 用于 ViewPager 中的图像

onClick for images in ViewPager

我在 ViewPager. 的帮助下制作了一个图像滑块 我想在 ViewPager 加载的不同图像中单击时显示不同的 toast 消息,我该怎么做?

这是我的 activity 显示图像滑块的位置:

public class MainActivity extends AppCompatActivity{
    public static final String MAIN = "OTHER";
    private static ViewPager mPager;
    private static int currentPage = 0;
    private static final Integer[] XMEN=
            {R.drawable.benz2, R.drawable.benz2,
                    R.drawable.benz2};
    private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
    @Override
    protected void onCreate(...);
        init();
}
private void init() {
        for(int i=0;i<XMEN.length;i++)
            XMENArray.add(XMEN[i]);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(new com.example.nepali_test.MyAdapter(MainActivity.this,XMENArray));
        CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(mPager);
        final Handler handler = new Handler();
        final Runnable Update = new Runnable() {
            public void run() {
                if (currentPage == XMEN.length) {
                    currentPage = 0;
                }
                mPager.setCurrentItem(currentPage++, true);
            }
        };
        Timer swipeTimer = new Timer();
        swipeTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(Update);
            }
        }, 3000, 3000);
    }

这是 viewpager 的适配器:

public class MyAdapter extends PagerAdapter {

    private ArrayList<Integer> images;
    private LayoutInflater inflater;
    private Context context;

    public MyAdapter(Context context, ArrayList<Integer> images) {
        this.context = context;
        this.images=images;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View myImageLayout = inflater.inflate(R.layout.slide, view, false);
        ImageView myImage = (ImageView) myImageLayout
                .findViewById(R.id.image);
        myImage.setImageResource(images.get(position));
        view.addView(myImageLayout, 0);
        return myImageLayout;
    }

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

我试着加入

if (currentPage==1){
            Toast.makeText(this, "page 1", Toast.LENGTH_SHORT).show();
        }

init() 但它没有用。

试试这个:

@Override
public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View page = inflater.inflate(R.layout.YOUR_PAGE, null);

    page.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + pos);
        }
    });


    ((ViewPager) collection).addView(page, 0);
    return page;
}

希望这会奏效!

在适配器中制作图片点击界面class

public class MyAdapter extends PagerAdapter {

private ArrayList<Integer> images;
private LayoutInflater inflater;
private Context context;
MyClassImage myClassImage;

public interface MyClassImage{
    void imageClick(String id);

}

public ViewPagerAdapter(Context context, ArrayList<Integer> images,MyClassImage myClassImage) {
    this.context = context;
    this.images=images;
    this.myClassImage=myClassImage;

    inflater = LayoutInflater.from(context);
}

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

@Override
public int getCount() {
    return images.size();
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
    View myImageLayout = inflater.inflate(R.layout.z_slide_pager_item, view, false);
    ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.image);
    myImage.setImageResource(images.get(position));
    view.addView(myImageLayout, 0);
    final int id=images.get(position);
    final String imgId=String.valueOf(id);


    myImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myClassImage.imageClick(imgId);
        }
    });
    return myImageLayout;
}

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

并在您的 MainActivity 上实现它 class

 public class MainActivity extends AppCompatActivity implements MyAdapter.MyClassImage {
public static final String MAIN = "OTHER";
private static ViewPager mPager;
private static int currentPage = 0;
private static final Integer[] XMEN= {R.drawable.benz2, R.drawable.benz2, R.drawable.benz2};
private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
@Override
protected void onCreate(...);
init();
}
    private void init() {
    for(int i=0;i<XMEN.length;i++)
        XMENArray.add(XMEN[i]);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new com.example.nepali_test.MyAdapter(MainActivity.this,XMENArray,this));
    CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(mPager);
    final Handler handler = new Handler();
    final Runnable Update = new Runnable() {
        public void run() {
            if (currentPage == XMEN.length) {
                currentPage = 0;
            }
            mPager.setCurrentItem(currentPage++, true);
        }
    };
    Timer swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(Update);
        }
    }, 3000, 3000);
}

@Override
public void imageClick(String id) {

    Toast.makeText(this, "page "+id, Toast.LENGTH_SHORT).show();
}

希望对您有所帮助。

谢谢。

根据我的理解,您需要在单击 ViewPager 的每个页面中的图像时显示吐司,在这种情况下,一种简单的方法是将 onClickListener 添加到您的 ImageView 在适配器的 instantiateItem 方法中。所以你应该 change/replace 你的方法是这样的:

@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup view, final int position) {
    View myImageLayout = inflater.inflate(R.layout.slide, view, false);
    ImageView myImage = (ImageView) myImageLayout
            .findViewById(R.id.image);
    myImage.setImageResource(images.get(position));
    myImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context.getApplicationContext(), "page "+position, Toast.LENGTH_SHORT).show();
        }
    });
    view.addView(myImageLayout, 0);
    return myImageLayout;
}