Mainactivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
// End Image slide show
}
ImageAdapter.java
package com.example.gallery;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
答案 0 :(得分:2)
对于计时器, 在main活动中,在viewPager.setAdapter(adapter);
之后添加这些myPager.setCurrentItem(0);
pagerAdapter.setTimer(myPager,7);
在寻呼机适配器类
中//gloabals
final Handler handler = new Handler();
public Timer swipeTimer ;
/**
* this function swipes pages left to right for every 7 seconds
* @param myPager its viewpager.
* @param time as second, for 7 second enter 7
*
*/
public void setTimer(final ViewPager myPager, int time){
final int size =objects.size();
final Runnable Update = new Runnable() {
int NUM_PAGES =size;
int currentPage = 0 ;
public void run() {
if (currentPage == NUM_PAGES ) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
或者你可以使用这个
/**
* this function swipes pages left to right for every 7 seconds
* @param myPager
* @param time
* @param numPages we recommend that it should be as much as much objects.size()
* @param curPage we recommend that it should start from 0
*
*/
public void setTimer(final ViewPager myPager, int time, final int numPages, final int curPage){
final Runnable Update = new Runnable() {
int NUM_PAGES =numPages;
int currentPage = curPage ;
public void run() {
if (currentPage == NUM_PAGES ) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
这是用于停止计时器
/**
* its kills runnable
*/
public void stopTimer(){
//handler.removeCallbacks(null);
swipeTimer.cancel();
}