该应用程序正在使用FragmentStatePagerAdapter
和随附的ViewPager。我有3个片段。我试图在第3个片段ViewPager
中调用FragmentActivity类(SignUpActivity)中定义的onCreateView()
,并通过以下行将ViewPagers当前项设置为第一页:
SignUpActivity.pager.setCurrentItem(0));
以上行导致以下错误:
java.lang.IllegalStateException: Recursive entry to executePendingTransactions
但是通过实验,当我运行与上面相同的setCurrentItem(0)
行时,在setUserVisibleHint()
方法中,ViewPager
按预期显示了第0个片段。为什么ViewPager在第一种情况下失败,我假设ViewPager
在完成之前的任何调用之前都无法调用?
我如何能够创建一种机制来从ViewPager
和FragmentActivity
调用setCurrentItem(x)
到我想要的任何视图?
SignUpActivity代码(FragmentActivity):
public class SignUpActivity extends FragmentActivity {
CustFSPA adapter;
static ViewPager pager;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up); /
adapter = new CustFSPA(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.signUpPager);
pager.setAdapter(adapter);
}
public static class CustFSPA extends FragmentStatePagerAdapter{
int numberFragments = 3;
static FragmentManager fragmentManager;
public CustFSPA(FragmentManager manager){
super(manager);
fragmentManager = manager;
}
@Override
public int getCount(){
return numberFragments;
}
@Override
public Fragment getItem(int position){
switch(position){
case 0:
// will display SignUpFragmentFirst
return SignUpFragmentFirst.calledFromPager();
case 1:
return SignUpFragmentSecond.calledFromPager();
default:
return SignUpFragmentThird.calledFromPager();
}
}
}
第三段片段代码:(与我提供前两个片段'代码无关)
public class SignUpFragmentThird extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
System.out.println("onCreate third");
}
@Override
public void onStart(){
super.onStart();
System.out.println("Starting 3rd");
}
@Override
public void onPause(){
super.onPause();
System.out.println("Paused 3rd");
}
@Override
public void onResume(){
super.onResume();
System.out.println("Resumed 3rd");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
System.out.println("onCreateView third");
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.signUpPager);
SignUpActivity.pager.setCurrentItem(0); <======= THIS WAS THE FIRST ATTEMPT WHICH FAILED AND RESULTED IN THE ERROR
View view = inflater.inflate(R.layout.sign_up_fragment_third, container, false);
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
System.out.println("THIRD is visible to the user");
SignUpActivity.pager.setCurrentItem(1); <========= SECOND ATTEMPT WHICH WOKRED SUCCESSFULLY
} else {
System.out.println("THIRD is not visible to the user");
}
}
static SignUpFragmentThird calledFromPager(){
System.out.println("third case def " + SignUpActivity.pager.getCurrentItem());
System.out.println("calledFromPager third");
SignUpFragmentThird fragment = new SignUpFragmentThird();
return fragment;
}
}
谢谢
答案 0 :(得分:0)
您可以通过此调用将viewPager设置为任何所需的视图。基本上,此侦听器会跟踪viewpager的页面。你需要在Parent Activity中实现它。
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
Log.i("TAG", "position: " + position);
//Here you can change the position of the viewPager.
//you have three fragnment so when the position would be positon=2 then you can set viewPager to any view
switch(position)
case 2:
pager.setCurrentItem(0));
break;
}
});
希望它会有所帮助。