gpt4 book ai didi

java - 如何解决Android中图像 slider 移动过快的问题?

转载 作者:行者123 更新时间:2023-12-01 06:01:43 25 4
gpt4 key购买 nike

在我的 Android 项目中,我创建了一个图像 slider 。图像 slider 包含两个图像,滑动时长为 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) {
}
});

}

这是我用来设置图像 slider 适配器的适配器类 -

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;
}
}

现在,问题是,我的 Activity 中有一个滑动刷新。因此,当我滑动屏幕刷新时,图像 slider 的行为方式有所不同。图像移动速度非常快,不遵循设置的延迟和周期时间。

我不知道为什么会出现这个问题以及如何解决这个问题。因此,如果有人发现问题并提出解决方案,那就太好了。

最佳答案

引入设置图像滑动:

 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) {
}
});

}

关于java - 如何解决Android中图像 slider 移动过快的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com