我使用的ViewPager具有从服务器获取的动态页数。还在viewpager中显示当前自动旋转页面的点图像。但滚动到最后一页后,它会卡住并停止旋转页面。 请帮助,我想要轮流旋转它。
final AdCarasouleAdapter adAdapter = new AdCarasouleAdapter(getActivity(), carousels);
adView.setAdapter(adAdapter);
adAdapter.notifyDataSetChanged();
adView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.i(TAG, "onPageSelected " + position);
currentSlideNumber = position;
showCarouselSlide(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
// for rotation
if( carousels.size() > 1 )
rotateCarousel();
rotateCarousel方法
private void rotateCarousel() {
if (carouselDaemon != null) {
carouselDaemon.shutdown();
carouselDaemon = null;
}
carouselDaemon = new ScheduledThreadPoolExecutor(1);
// This process will execute immediately, then execute every 3 seconds.
carouselDaemon.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
HomeFragment.this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showCarouselSlide(currentSlideNumber);
if (++currentSlideNumber > maxSlides - 1)
currentSlideNumber = 0;
}
});
}
}, 0L, 4000L, TimeUnit.MILLISECONDS);
}
showCarouselSlide方法
private void showCarouselSlide(int position){
for (int i = 0; i < carousels.size(); i++) {
int dot = dotsArray[i];
ImageView dotView = (ImageView) view.findViewById(dot);
dotView.setImageResource(R.drawable.dots);
}
Application.currentHomeCarouselSlideNo = position;
try {
ImageView dotSelectedView = (ImageView) view.findViewById(dotsArray[position]);
dotSelectedView.setImageResource(R.drawable.dots_select);
ImageView image = (ImageView) view.findViewById(R.id.carousel_image);
Picasso.with(getActivity()).load(carousels.get(position).getBannerName()).fit().into(image);
}
catch(Exception e) {}
}