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.
Looks like you are creating a ViewPager inside a fragment.
In this case getFragmentManager()
should be getChildFragmentManager()
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments