我正在开发一个应用程序,我必须使用viewpager,在viewpager中的所有项目完成后,我必须调用一个活动.我无法为此获得事件监听器.这也是我一直在推荐的内容:
https://github.com/chiuki/android-swipe-image-viewer/blob/master/src/com/sqisland/android/swipe_image_viewer/MainActivity.java
https://github.com/chiuki/android-swipe-image-viewer/blob/master/src/com/sqisland/android/swipe_image_viewer/MainActivity.java
这是我到目前为止所做的:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); ImagePagerAdapter adapter = new ImagePagerAdapter(); viewPager.setAdapter(adapter); OnPageChangeListener mListener = new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0,float arg1,int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { Log.i("Doing something here","On Scroll state changed"); } }; viewPager.setOnPageChangeListener(mListener); } private class ImagePagerAdapter extends PagerAdapter { private int[] mImages = new int[] { R.drawable.libin1,R.drawable.libin2 }; @Override public int getCount() { return mImages.length; } @Override public boolean isViewFromObject(View view,Object object) { return view == ((ImageView) object); } @Override public Object instantiateItem(ViewGroup container,int position) { Context context = MainActivity.this; ImageView imageView = new ImageView(context); int padding = context.getResources().getDimensionPixelSize( R.dimen.padding_medium); imageView.setPadding(padding,padding,padding); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setImageResource(mImages[position]); ((ViewPager) container).addView(imageView,0); return imageView; } @Override public void destroyItem(ViewGroup container,int position,Object object) { ((ViewPager) container).removeView((ImageView) object); } } }
我的问题是如果viewpager中的所有项目都已完成,如何获取事件监听器.
任何形式的帮助将不胜感激.
解决方法
private OnPageChangeListener mListener = new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub selectedIndex = arg0; } boolean callHappened; @Override public void onPageScrolled(int arg0,int arg2) { // TODO Auto-generated method stub if( mPageEnd && arg0 == selectedIndex && !callHappened) { Log.d(getClass().getName(),"Okay"); mPageEnd = false;//To avoid multiple calls. callHappened = true; }else { mPageEnd = false; } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub if(selectedIndex == adapter.getCount() - 1) { mPageEnd = true; } } }; ViewPager.setOnPageChangeListener(mListener);
onPageScrolled或onPageSelected您可以在此处使用任何这些,并且还检查所选页面是否等于ViewPager中的项目数.