use Viewpager to increment variable

Asked

Viewed 162 times

1

well what I wanted was for my Viewpager to increment a variable within the Activity in which it was executed. Is that possible? just find documentation for different layouts

my application has 1 page that I simulate others, not to load too much and for needing 60 pages, I created a single layout, that when the user changes page I increment a global variable that changes all my textviews and 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"

  • I believe you’re referring to Viewpager, right? You have something to implement on it like multiple pages or simply want to increment this variable?

  • so, my application has 1 page that I simulate others, not to load too much and by needing 60 pages, I created a single layout, that when the user changes page I increment a global variable that changes all my textviews and 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"

  • @Joannis, put this information in your question, to make it clear.

1 answer

1


Well, what I did very quickly here was totally based on example that I pointed out to you in the comment. So, to simplify, I suggest following the beginning of the example in which he mounts the ViewPager.

Done this, as you will have only one Fragment but with different data, in the class Screenslidepagefragment add a parameter and the constructor by 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 etc.

Returning now on the Screenslidepagerarpter, you need to pass the page number on your new constructor, so now its 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;
    }
}

You can do the download of this project I did and test yourself (ignore the project name).

  • vlw-face. so I doubt now that I have a public class Mainactivity extends Actionbaractivity and you have created a public class Mainactivity extends Fragmentactivity

  • ActionBarActivity stretches FragmentActivity, then you can use the first one without problem. I used the FragmentActivity because that’s what was in the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.