I have Nested Fragments
[1 main Fragment
called "MainFrag" and many child Fragments
]
the App can show MainFrag or can show other fragments.
what I want to do is to setCurrentItem
of MainFrag's ViewPager
every time the user selects to show that MainFrag so I tried to put it at it's onCreateView
but this only works for the first time the User Shows MainFrag as if user Selects another fragment and then re-selects MainFrag the seCurrentItem
is not working
I also tried to put it inside MainFrag's onResume
, It worked as required but it also causes a problem that if the user moved the App to background and then reOpen it viewPager will Scroll and i don't want this
So where Exactly Can I put setCurrentItem
?
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
Edit: I am using Navigation Menu to Move between 3 main fragments where MainFrag is one of them.
答案 0 :(得分:1)
After your mViewPager.setAdapter(adapter);
method you can put
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
答案 1 :(得分:1)
将适配器设置为viewpager后添加setCurrentItem方法
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
答案 2 :(得分:0)
试试这个:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}