Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Count of ViewPager

How do you unlock the ability to slide to a new page after clicking a button on a previous page?

Currently I have a PagerAdapter. The code below instantiates the item. Within the getCount(), a value of 3 is returned and thus 3 slides are made. I was wondering if there was a way to only have 3 views, but after clicking a button, unlock a 4th view that you can now slide to?

@Override
    public Object instantiateItem(final View collection, int position) {
        final LayoutInflater inflater = (LayoutInflater) TViewPager.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView tv = new TextView(TViewPager.this);

//Describe the separate layouts of views
switch (position) {
        case 1:
            View v1 = inflater.inflate(R.layout.expander2, null, false);
                ((ViewPager) collection).addView(v1, 0);
                final Button button = (Button) findViewById(R.id.button_click_me);
                button.setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v1) {
                         Toast.makeText(getBaseContext(), "+1", Toast.LENGTH_LONG).show();
                        //*Some code*//

                     }
                 });
                return v1;

Is there a way to change the:

public int getCount() {
        return 3; 
    }

Like a "setCount()" and then place it at the //* some code *// to increase the amount of slides?

Would adding

instantiateItem(collection,4);

work somewhere work?

Thanks,

like image 309
d.mc2 Avatar asked Feb 21 '12 01:02

d.mc2


1 Answers

Here's how I solved this problem. To clarify, how do you click a button and get more Pages that you can scroll into. I made:

private int NUM_VIEWS = 2;

public void setN(int N) {
    this.NUM_VIEWS = N;
}

and then I changed an important line.

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

My clickListener is

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v1) {
         myAdapter.setN(3);
         myPager.setCurrentItem(2);
        }
}); 

I added another case to facilitate the new item. Afterwards when I click the button, my pageadapter will expand to 3 views instead of 2 and the new case will be the new view.

like image 127
d.mc2 Avatar answered Sep 21 '22 13:09

d.mc2