我尝试获取当前页面位置以便使用它来设置壁纸等等我尝试在我的SimpleOnPageChangeListener
上使用Viewpager
通过onPageSelected
获取位置问题是在滑动页面时调试位置正在改变但是当我尝试使用所选位置时它总是返回0
这是我的代码
public class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
int currentPage;
Context context;
List<String> lol;
public MyPageChangeListener(Context context, List<String> lol) {
this.context = context;
this.lol=lol;
}
@Override
public void onPageSelected(int position) {
this.currentPage = position;
}
public int getCurrentPage() {
return currentPage; // always returns 0
}
}
public class ImagePagerActivity extends Activity {
private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
ImageLoader imageLoader;
InputStream inputStream2;
List<String> lol;
private Bitmap bitmapz;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
Intent i = getIntent();
List<String> lol = i.getStringArrayListExtra("lol");
this.lol = lol;
int pagerPosition = i.getExtras().getInt("id");
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_stub)
.showImageOnFail(R.drawable.ic_stub)
.resetViewBeforeLoading()
.cacheOnDisc()
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300))
.build();
pager = (ViewPager) findViewById(R.id.myfivepanelpager);
pager.setAdapter(new ImagePagerAdapter(lol));
pager.setCurrentItem(pagerPosition);
registerForContextMenu(pager);
pager.setOnPageChangeListener(new MyPageChangeListener());
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}
private void Setwallpaper() throws IOException {
MyPageChangeListener myPageChangeListener = new MyPageChangeListener();
int position = myPageChangeListener.getCurrentPage();
InputStream inputStream2 = getAssets().open(lol.get(position));
setWallpaper(inputStream2);
}
private class ImagePagerAdapter extends PagerAdapter {
private List<String> images;
private LayoutInflater inflater;
private ImageLoader imageLoader;
private int nposition;
private Bitmap zBitmap;
private View imageLayout;
ImagePagerAdapter(List<String> lol) {
this.images = lol;
inflater = getLayoutInflater();
ImageLoader imageLoader = ImageLoader.getInstance();
this.imageLoader = imageLoader;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public void finishUpdate(View container) {
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image, container, false);
this.imageLayout = imageLayout;
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
String imString = images.get(position);
String imageUria = "assets://"+imString;
this.nposition = position;
imageLoader.displayImage(imageUria, imageView, options );
((ViewPager) container).addView(imageLayout, 0);
return imageLayout;
}
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
public void restoreState(Parcelable state, ClassLoader loader) {
}
public Parcelable saveState() {
return null;
}
public void startUpdate(View container) {
}
}