I have a ViewPager which swipes between Fragments. I\'m using a FragmentStatePagerAdapter to feed the Fragments to the ViewPager. If the user swipes left at a normal pace, and t
Extremely late reply, but if someone is having trouble, this is what worked for me. I was wrapping the ViewPager inside View with custom styling like this:
<View style={{bla bla bla style here}}>
<ViewPager>
<Layout></Layout>
<Layout></Layout>
</ViewPager>
</View>
I just removed <View>
from the file and ViewPager fixed itself.
<ViewPager>
<Layout></Layout>
<Layout></Layout>
</ViewPager>
so what I am saying is, try removing some other parent layout tags, maybe they are causing this problem.
PS: this worked in react-native but I hope it helps in other realms as well.
I just realized that you're doing a lot of UI work in onCreate
() in the main Activity. It is more proper to do the work in onCreateView
(). I believe the Android framework is not finish doing the UI work in onCreate
() and therefore you see incomplete UI rendering.
I know this is not clearly stated in Android documentation. If you check other SO posts or sample projects, other developers do little UI work in onCreate
(). At least, the layouts are simpler than yours.
Here is my suggestion.
Inflate the fragtest
layout in an Activity or Fragment in method onCreateView
(), using the ID listed on post. Notice the override method only inflates.
Sample code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragtest, container, false);
}
On a Fragment, start accessing the UI elements and the ViewPager, using the ID listed on post. Sample code:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mProfilesViewPager = (ViewPager) findViewById(R.id.viewPager);
...
I've noticed that I see this issue if I have some animations given by the animateLayoutChanges. Just deactivating it in the xml file, prevents the pages to be stuck in the middle.
For now, I suspect two methods.
1) In the fragment code:
@Override
public int getCount()
{
return mBagels.size();
}
Note: getCount()
should return the number of fragments instead of the size of the list. I could not tell how many of fragments you will have. Perhaps you have to keep track in the Adapter.
2) Another, I suspect getItem()
method and the use of newInstance
(). The related specific code:
FragmentProfile fragment = FragmentProfile.newInstance(mBagels.get(i),...
Notes:
According to Google webpage FragmentStatePagerAdapter, method newInstance
should create a new fragment probably because that's when the FragmentStatePagerAdapter does not have the fragment in memory yet OR was released from memory.
Perhaps post the code related to FragmentProfile.newInstance
especially if you disagree with my statement above.
In my case, the problem was an empty Fragment. After create a fragment with a layout, it's starts working as expected.
Basically, I used a empty fragment for test the view:
fragment = new Fragment(); //Strange behavior in ViewPager
When I used the final fragment which has a layout, the behavior was the correct:
fragment = MyFragment.newInstance(); //Correct behavior
I know that this response doesn't answer the current question, but some people with similar problems arrive here. So, I hope it's helpful.
I also encountered this problem, my solution below:
mainViewPager.post { mainViewPager.setCurrentItem(item, false) }