使用 viewPager 放映幻灯片
slide show with viewPager
我正在尝试用 viewPager
创建一个 slide-show
。我的问题是 slide-show
的第一项即使使用 Picasso
库也会在大约 3 秒后出现,并且图像也存储在本地,我在 3 个真实设备上测试了它,结果是一样的,如何我应该解决这个问题吗?
activity代码:
private void slider() {
CircleIndicator indicator = (CircleIndicator) findViewById(R.id.ma_slide_indicator);
sData=new ArrayList<>();
sData.add(new Slider_Model("item 1",31235545,R.drawable.angrybirds));
sData.add(new Slider_Model("item 2",12225545,R.drawable.asphalt8));
sData.add(new Slider_Model("item 3",45522545,R.drawable.clashofclans));
sData.add(new Slider_Model("item 4",71225222,R.drawable.fruitninja));
sData.add(new Slider_Model("item 5",91000220,R.drawable.talkingtom));
sData.add(new Slider_Model("item 6",100022002,R.drawable.cuttherope));
sData.add(new Slider_Model("item 7",5555445,R.drawable.pou));
Slide_Adapter slide_adapter = new Slide_Adapter(this,sData);
viewPager.setAdapter(slide_adapter);
viewPager.setOffscreenPageLimit(2);
indicator.setViewPager(viewPager);
NUM_PAGES=sData.size();
Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
};
timing(new Handler(),Update,4000,4000);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.v(TAG, "SCROLL POS " + position);
currentPage = position;
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void timing(final Handler h, final Runnable run, int delay, int period){
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
h.post(run);
}
}, delay, period);
}
和适配器:
public class Slide_Adapter extends PagerAdapter {
private static final String TAG ="value" ;
private Context mContext;
private LayoutInflater Inflater;
private ArrayList<Slider_Model> sData;
public Slide_Adapter(Context mContext, ArrayList<Slider_Model> data) {
this.mContext = mContext;
this.Inflater=LayoutInflater.from(mContext);
this.sData=data;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v=Inflater.inflate(R.layout.slideshow_layout,container,false);
assert v !=null;
ImageView imageView=(ImageView) v.findViewById(R.id.slide_imageView);
TextView title=(TextView) v.findViewById(R.id.slide_title);
TextView phone=(TextView) v.findViewById(R.id.slide_ph);
title.setText(sData.get(position).getTitle());
phone.setText(String.valueOf(sData.get(position).getPhone()));
Picasso.get().load(sData.get(position).getImage()).into(imageView);
container.addView(v,0);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return sData.size();
}
}
由于我们的 currentPage 是全局变量,请确保其已正确初始化为零。然后你需要在启动定时器之前设置当前项目:
...
viewPager.setCurrentItem(currentPage, true);
Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
};
...
我正在尝试用 viewPager
创建一个 slide-show
。我的问题是 slide-show
的第一项即使使用 Picasso
库也会在大约 3 秒后出现,并且图像也存储在本地,我在 3 个真实设备上测试了它,结果是一样的,如何我应该解决这个问题吗?
activity代码:
private void slider() {
CircleIndicator indicator = (CircleIndicator) findViewById(R.id.ma_slide_indicator);
sData=new ArrayList<>();
sData.add(new Slider_Model("item 1",31235545,R.drawable.angrybirds));
sData.add(new Slider_Model("item 2",12225545,R.drawable.asphalt8));
sData.add(new Slider_Model("item 3",45522545,R.drawable.clashofclans));
sData.add(new Slider_Model("item 4",71225222,R.drawable.fruitninja));
sData.add(new Slider_Model("item 5",91000220,R.drawable.talkingtom));
sData.add(new Slider_Model("item 6",100022002,R.drawable.cuttherope));
sData.add(new Slider_Model("item 7",5555445,R.drawable.pou));
Slide_Adapter slide_adapter = new Slide_Adapter(this,sData);
viewPager.setAdapter(slide_adapter);
viewPager.setOffscreenPageLimit(2);
indicator.setViewPager(viewPager);
NUM_PAGES=sData.size();
Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
};
timing(new Handler(),Update,4000,4000);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.v(TAG, "SCROLL POS " + position);
currentPage = position;
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void timing(final Handler h, final Runnable run, int delay, int period){
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
h.post(run);
}
}, delay, period);
}
和适配器:
public class Slide_Adapter extends PagerAdapter {
private static final String TAG ="value" ;
private Context mContext;
private LayoutInflater Inflater;
private ArrayList<Slider_Model> sData;
public Slide_Adapter(Context mContext, ArrayList<Slider_Model> data) {
this.mContext = mContext;
this.Inflater=LayoutInflater.from(mContext);
this.sData=data;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v=Inflater.inflate(R.layout.slideshow_layout,container,false);
assert v !=null;
ImageView imageView=(ImageView) v.findViewById(R.id.slide_imageView);
TextView title=(TextView) v.findViewById(R.id.slide_title);
TextView phone=(TextView) v.findViewById(R.id.slide_ph);
title.setText(sData.get(position).getTitle());
phone.setText(String.valueOf(sData.get(position).getPhone()));
Picasso.get().load(sData.get(position).getImage()).into(imageView);
container.addView(v,0);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return sData.size();
}
}
由于我们的 currentPage 是全局变量,请确保其已正确初始化为零。然后你需要在启动定时器之前设置当前项目:
...
viewPager.setCurrentItem(currentPage, true);
Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
};
...