@BindView(R.id.viewPager) ViewPager viewPager; @BindView(R.id.ivPre) ImageView ivPre; @BindView(R.id.ivNext) ImageView ivNext; private int[] imagePaths = { R.mipmap.scenery1, R.mipmap.scenery2, R.mipmap.scenery3, R.mipmap.scenery4, R.mipmap.scenery5 }; ArrayList imageViewList; private MyPagerAdapter myPagerAdapter; private int currentPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_vptest3); ButterKnife.bind(this); initData(); } @OnClick({R.id.ivPre, R.id.ivNext}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.ivPre: currentPosition -= 1; viewPager.setCurrentItem(currentPosition); break; case R.id.ivNext: currentPosition += 1; viewPager.setCurrentItem(currentPosition); break; } } private void initData() { imageViewList = new ArrayList<>(); viewPager.setCurrentItem(0); setDataToViewPager(); } /** • 把数据设置到 ViewPager 上 */ private void setDataToViewPager() { // 将图片放到集合中 imageViewList = new ArrayList<>(); for (int imagePath : imagePaths) { ImageView imageView = new ImageView(this); imageView.setImageResource(imagePath); // Glide.with(this).load(imagePath).into(imageView); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageViewList.add(imageView); } // 进行适配 myPagerAdapter = new MyPagerAdapter(); if (viewPager != null) { viewPager.setAdapter(myPagerAdapter); } // 第一次进入 不显示前置箭头 ivPre.setVisibility(View.GONE); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { currentPosition = position; // 第一个位置 不显示前置箭头 ivPre.setVisibility(position == 0 ? View.GONE : View.VISIBLE); // 最后一个位置 不显示后置箭头 ivNext.setVisibility(position == imageViewList.size() - 1 ? View.GONE : View.VISIBLE); } @Override public void onPageScrollStateChanged(int state) { } }); } /** • 自定义适配器 */ private class MyPagerAdapter extends PagerAdapter { @Override public int getCount() {