android viewpager轮播图,最后一页再次向左滑动,到第一页,在第一页时再次向右滑动,到最后一页,完整代码
时间: 2023-10-03 10:02:18 浏览: 190
下面是一个简单的Android ViewPager轮播图实现,包括最后一页向左滑动到第一页和第一页向右滑动到最后一页的功能。
1. 首先,在你的布局文件中,添加ViewPager和指示器(Indicator):
```
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:id="@+id/dotsIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="16dp"
app:dotsColor="@color/white"
app:dotsCornerRadius="8dp"
app:dotsSize="8dp"
app:dotsSpacing="8dp"/>
```
2. 创建一个适配器(Adapter)来管理ViewPager中的视图:
```
public class ViewPagerAdapter extends PagerAdapter {
private Context mContext;
private List<Integer> mImageList;
public ViewPagerAdapter(Context context, List<Integer> imageList) {
mContext = context;
mImageList = imageList;
}
@Override
public int getCount() {
return mImageList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageList.get(position));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((ImageView) object);
}
}
```
3. 在Activity或Fragment中,初始化ViewPager和指示器,并设置适配器:
```
ViewPager viewPager = findViewById(R.id.viewPager);
DotsIndicator dotsIndicator = findViewById(R.id.dotsIndicator);
List<Integer> imageList = new ArrayList<>();
imageList.add(R.drawable.image1);
imageList.add(R.drawable.image2);
imageList.add(R.drawable.image3);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageList);
viewPager.setAdapter(adapter);
dotsIndicator.setViewPager(viewPager);
```
4. 添加一个OnPageChangeListener来监听ViewPager的滑动事件,并在最后一页和第一页时执行特殊的操作:
```
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == imageList.size() - 1) {
// 最后一页向左滑动到第一页
viewPager.setCurrentItem(0, false);
} else if (position == 0) {
// 第一页向右滑动到最后一页
viewPager.setCurrentItem(imageList.size() - 1, false);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
```
完整代码如下:
```
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.viewPager);
DotsIndicator dotsIndicator = findViewById(R.id.dotsIndicator);
List<Integer> imageList = new ArrayList<>();
imageList.add(R.drawable.image1);
imageList.add(R.drawable.image2);
imageList.add(R.drawable.image3);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageList);
viewPager.setAdapter(adapter);
dotsIndicator.setViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == imageList.size() - 1) {
// 最后一页向左滑动到第一页
viewPager.setCurrentItem(0, false);
} else if (position == 0) {
// 第一页向右滑动到最后一页
viewPager.setCurrentItem(imageList.size() - 1, false);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public class ViewPagerAdapter extends PagerAdapter {
private Context mContext;
private List<Integer> mImageList;
public ViewPagerAdapter(Context context, List<Integer> imageList) {
mContext = context;
mImageList = imageList;
}
@Override
public int getCount() {
return mImageList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageList.get(position));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((ImageView) object);
}
}
}
```
阅读全文