Changing Count of ViewPager

后端 未结 2 1177
野的像风
野的像风 2021-01-05 11:07

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 ite

相关标签:
2条回答
  • 2021-01-05 11:29

    getCount by default returns the number of child views of your ViewPager. By doing pager.add(layout_item) you can add a new item to the pager (of course you have to inflate it somewhere first). getCount will automatically adjust.

    0 讨论(0)
  • 2021-01-05 11:45

    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.

    0 讨论(0)
提交回复
热议问题