Starting position PagerAdapter

I use the following example to embed my viewPager: http://code.google.com/p/viewpagerexample/issues/list

The problem with this example is that I cannot figure out how to set the starting position, the default starting position is 0. In principle, I cannot control whether there is an available left or right view.

Is there a way to control the center View the current position? Is there a better way to do this? is it possible to make it circular?

+67
android android-viewpager swipe
Aug 09 '12 at 8:17
source share
8 answers

I found a way to set its position, which runs outside the class:

awesomePager = (ViewPager) findViewById(R.id.awesomepager); awesomePager.setAdapter(awesomeAdapter); awesomePager.setCurrentItem(CurrentPosition); 

and it can be limited by calculating the number of elements that I want to insert into it

+155
Aug 09 '12 at 10:00
source share

I noticed that if you update an Activity (changing orientation) using a ViewPager that has a FragmentStatePagerAdapter, then the Adapter will use its Fragments again. A way to stop this:

 @Override public void onSaveInstanceState(Bundle savedInstanceState) { if (viewPager != null) { // before screen rotation it better to detach pagerAdapter from the ViewPager, so // pagerAdapter can remove all old fragments, so they're not reused after rotation. viewPager.setAdapter(null); } super.onSaveInstanceState(savedInstanceState); } 

but then after Activity Activity ViewPager alwayes opens page 0 first and setCurrentItem (CurrentPosition); does not work. Fixed for this page change after delay:

 new Handler().postDelayed(new Runnable() { @Override public void run() { viewPager.setCurrentItem(newPosition); } }, 100); 
+19
Dec 10 '13 at 13:13
source share

To start with the last snippet, I did the following:

 PagerAdapter pagerAdapter = new PagerAdapter(); viewPager.setAdapter(pagerAdapter); viewPager.setCurrentItem(pagerAdapter.getCount() - 1); 
+16
Mar 11 '15 at 14:33
source share

I ran into a problem: if I installed the current element before I installed the adapter, the first element that I will return will always be at position 0.

Make sure you do:

 awesomePager.setAdapter(awesomeAdapter); awesomePager.setCurrentItem(CurrentPosition); 

and not:

 awesomePager.setCurrentItem(CurrentPosition); awesomePager.setAdapter(awesomeAdapter); 
+8
Nov 25 '13 at 22:19
source share

I have an array larger than 1000, and in the dymanic viewpager I ran into a left hook stuck on first boot. In the encoder below, this was resolved and resulted in a smooth scrolling:

 @Override onResume(){ super.onResume(); viewPager.setCurrentItem(newPosition); } 
+6
Nov 03 '15 at 7:09
source share

I use 3 fragments and when the application starts, the second ( middle ) fragment will be shown by default. I just use the onResume function and everything works fine.

 @Override protected void onResume() { super.onResume(); m_viewPager.setCurrentItem(1); } 
+4
Nov 28 '14 at 21:46
source share

I ran into the same problem. When I initialize the ViewPager, the position of the indicator was 0. This may depend on the number of calculations for the contents of the pager. I am using ViewTreeObserver as shown below.

 mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mViewPager.setCurrentItem(newPosition); removeOnGlobalLayoutListener(mSlidingTabLayout.getViewTreeObserver(), mGlobalLayoutListener); } }; mSlidingLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener); 

and

 private void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener) { if (observer == null) return; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { observer.removeGlobalOnLayoutListener(listener); } else { observer.removeOnGlobalLayoutListener(listener); } } 

Thus, you also never have to worry about setting the delay time.

+2
Nov 11 '15 at 10:05
source share
 @Override public Object instantiateItem(ViewGroup container, int position) { View currentPage = null; switch(position){ case 0: currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null) break; case 1: currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null) ///////////// This page will be default //////////////////// ((ViewPager)container).setCurrentItem(position); //////////////////////////////////////////////////////////// break; case 2: currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null) break; return currentPage; } 
+1
02 Sep '14 at 10:39
source share



All Articles