Search code examples
javaandroidandroid-layoutandroid-viewpagerswipe

Call activity after viewpager is finished


I'm developing an application in which I have to use viewpager and after all items in viewpager is finished I have to call an activity. I'm not able to get event listener for this. Here is what I have been refering too: https://github.com/chiuki/android-swipe-image-viewer/blob/master/src/com/sqisland/android/swipe_image_viewer/MainActivity.java

Here is what I have done so far:

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

My question is how to get event listener if all the items in viewpager is finished.


Solution

  • 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, float arg1, 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 or onPageSelected any of these you can use here and also check the selected page is equals to the number of items in the ViewPager.