在我的Android项目中,我创建了一个图像滑块。 “图像”滑块包含两个图像,滑动的持续时间为5000毫秒,图像停留时间为5000毫秒。
这是我介绍的设置图像滑动的方法-
private void init() {
final float density =
mContext.getResources().getDisplayMetrics().density;
mPager.setAdapter(new SlidingImage_Adapter(mContext,coverImageList));
indicator.setRadius(5 * density);
NUM_PAGES =coverImageList.size();
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 5000, 5000);
indicator.setOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
这是我用来为“图像”滑块设置适配器的适配器类-
SlidingImage_Adapter.java
public class SlidingImage_Adapter extends PagerAdapter {
private List<ModelBanner> imageModelArrayList;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context, List<ModelBanner> imageModelArrayList) {
this.context = context;
this.imageModelArrayList = imageModelArrayList;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return imageModelArrayList.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.slidingimages_layout, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Picasso.with(context).load(imageModelArrayList.get(position).getImagePath()).into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
现在,问题是,我在“活动”中进行了刷卡刷新。因此,当我滑动屏幕进行刷新时,图像滑块的行为会有所不同。图像移动非常快,没有遵循设置的延迟和周期时间。
我不知道为什么会出现此问题以及如何解决此问题。因此,如果有人发现问题并提出解决方案,那就太好了。
答案 0 :(得分:-1)
介绍如何设置图像滑动:
private void init() {
final float density =
mContext.getResources().getDisplayMetrics().density;
mPager.setAdapter(new SlidingImage_Adapter(mContext,coverImageList));
indicator.setRadius(5 * density);
NUM_PAGES =coverImageList.size();
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 5000, 5000);
indicator.setOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}