Using viewer page to switch between two fragments - however it disappears after switching into a fragment

纵饮孤独 提交于 2020-01-16 13:20:10

问题


I have a problem with my viewer page, so in this java file that I have which extends from Fragment has some code to do with view pager. Basically in my screen I want to let the user to swipe cross to the right and swipe across to the left. It works fine as it should be however I have other fragments too within the project I am working on so if I go to a different fragment and come back to this screen which lets me do this swipe feature, all for a sudden everything disappears from the screen. Any idea how to fix this issue ??

This is the java file;

public class ExampleFragment extends Fragment {
   View v;
   ViewPager viewPager;
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

      final View v =  inflater.inflate(R.layout.tab2_main, container, false);

      viewPager = (ViewPager)v.findViewById(R.id.accountswitching);
      AccountSwitching accountSwitching = new AccountSwitching(getFragmentManager());
      viewPager.setAdapter(accountSwitching);

This is the code I have inside the AccountSwitching java class;

public class AccountSwitching extends FragmentPagerAdapter {
public AccountSwitching(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {

    switch (i){
        case 0:
            return new FirstPage();
        case 1:
            return new SecondPage();
    }

    return null;
}

@Override
public int getCount() {
    return 2;
}

}

So as you saw on the image I attached on this post, where it says hello, I have got an onclick which is in the FirstPage. java file

FirstPage. java file ;

public class FirstPage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.home_screenmain_first, container, false);

    v.findViewById(R.id.first).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
            ((FrameLayout)getActivity().findViewById(android.R.id.tabcontent)).removeAllViews();
            transaction.replace(android.R.id.tabcontent, new ChangeScreen(), "ChangeScreen");
            transaction.commit();
        }
    });

    return v;
}

}

return v;
   }
}

This is a screen show of something from the viewer page is showing on the screen Example of what I am seeing before switching between fragments

This is what I see after switching between fragments After switching between fragments

Please tell me if you want to see any other code, not sure which code to send here that's why I didn't share it on here.


回答1:


Looks like you are creating a ViewPager inside a fragment.

In this case getFragmentManager() should be getChildFragmentManager().



来源:https://stackoverflow.com/questions/29588659/using-viewer-page-to-switch-between-two-fragments-however-it-disappears-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!