How to change ViewPager's page?

后端 未结 5 625
暗喜
暗喜 2020-11-30 22:23

I\'m using ViewPager in my app and define it in the main Activity. Inside onCreate method I load some number of pages from SharedPreferences and then pass it to

相关标签:
5条回答
  • 2020-11-30 22:39

    slide to right

    viewPager.arrowScroll(View.FOCUS_RIGHT);

    slide to left

    viewPager.arrowScroll(View.FOCUS_LEFT);

    0 讨论(0)
  • 2020-11-30 22:39

    Supplemental answer

    I was originally having trouble getting a reference to the ViewPager from other class methods because the addOnTabSelectedListener made an anonymous inner class, which in turn required the ViewPager variable to be declared final. The solution was to use a class member variable and not use the anonymous inner class.

    public class MainActivity extends AppCompatActivity {
    
        TabLayout tabLayout;
        ViewPager viewPager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            tabLayout = (TabLayout) findViewById(R.id.tab_layout);
            tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
            tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
            tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    
            viewPager = (ViewPager) findViewById(R.id.pager);
            final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    
            // don't use an anonymous inner class here
            tabLayout.addOnTabSelectedListener(tabListener);
    
        }
    
        TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
    
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        };
    
        // The view pager can now be accessed here, too.
        public void someMethod() {
            viewPager.setCurrentItem(0);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 22:40

    I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num ). That allows you to programatically switch to another page within the ViewPager.

    I'd need to see a stack trace from logcat to be more specific if this is not the problem.

    0 讨论(0)
  • 2020-11-30 22:50

    Without checking your code, I think what you are describing is that your pages are out of sync and you have stale data.

    You say you are changing the number of pages, then crashing because you are accessing the old set of pages. This sounds to me like you are not calling pageAdapter.notifyDataSetChanged() after changing your data.

    When your viewPager is showing page 3 of a set of 10 pages, and you change to a set with only 5, then call notifyDataSetChanged(), what you'll find is you are now viewing page 3 of the new set. If you were previously viewing page 8 of the old set, after putting in the new set and calling notifyDataSetChanged() you will find you are now viewing the last page of the new set without crashing.

    If you simply change your current page, you may just be masking the problem.

    0 讨论(0)
  • 2020-11-30 22:51

    for switch to another page, try with this code:

    viewPager.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            viewPager.setCurrentItem(num, true);
        }
    }, 100);
    
    0 讨论(0)
提交回复
热议问题