Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 975
Anupam Avatar asked Apr 11 '13 05:04

Anupam


People also ask

Should I use ViewPager or ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do I refresh ViewPager fragments?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

How many methods does the PageTransformer of a ViewPager expose?

PageTransformer interface and supply it to the ViewPager2 object. The interface exposes a single method, transformPage() . At each point in the screen's transition, this method is called once for each visible page (generally there's only one visible page) and for adjacent pages just off the screen.

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.


2 Answers

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.

like image 80
Triode Avatar answered Nov 14 '22 03:11

Triode


these three callbacks work like this:

  • onPageScrollStateChanged is called with state ViewPager.SCROLL_STATE_DRAGGING
  • onPageScrolled will be called many times, its parameter positionOffset and positionOffsetPixels are keep increasing
  • onPageScrolled's parameter positionOffset more than 0.5, onPageScrollStateChanged is called with state SCROLL_STATE_SETTLING
  • onPageSelected is called with next page index as position
  • onPageScrolled will be called many times, its parameter positionOffset and positionOffsetPixels are keep increasing
  • onPageScrolled's parameter positionOffset is 1, onPageScrollStateChanged is called with state SCROLL_STATE_IDLE

The logic should not put in onPageSelected and onPageScrollStateChanged, because with them you only know state is changed. only in onPageScrolled you can get the direction. So my implementation is like this:

        private int selectedPageIndex = -1;
        private boolean exitWhenScrollNextPage = false;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (exitWhenScrollNextPage && position == PAGE_COUNT - 1) {
                exitWhenScrollNextPage = false; // avoid call more times
                AndroidLog.error("-------- YEAH!");
            }
        }

        @Override
        public void onPageSelected(int position) {
            selectedPageIndex = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == SCROLL_STATE_IDLE) {
                exitWhenScrollNextPage = selectedPageIndex == PAGE_COUNT - 1;
            }

        }
like image 3
cn123h Avatar answered Nov 14 '22 03:11

cn123h