use ViewPager to increment variable

1

Well what I wanted was for my ViewPager to increment a variable within the Activity in which it was run. it is possible? I only find documentation for different layouts

My application has 1 page that I simulate others, not to load too much and because it needs 60 pages, I created a single layout, which when the user changes page I increment a global variable that changes all my textviews and the button texts according to the information contained in the database. so I wanted to use this ViewPager to increment this variable and so "change the page"

    
asked by anonymous 23.10.2014 / 20:31

1 answer

1

Well, what I did pretty quick here, was entirely based on the example I indicated for you in the comment. So for simplicity I suggest following the example start where it mounts ViewPager .

When you do this, you will only have a Fragment but with different data, in the ScreenSlidePageFragment class, add a parameter and the constructor passing the page number:

private int position;

public ScreenSlidePageFragment(int position) {
    this.position = position;
}

In this same class, in onCreateView you already have the page number and do what needs to be done, using the database and so on.

Going back now to the ScreenSlidePagerAdapter , you need to pass the page number to your new constructor, so now your adapter :

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new ScreenSlidePageFragment(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

If you'd like, you can download of this project I've done and test for yourself ( ignore the project name).

    
23.10.2014 / 23:17