Getting blank page on displaying fragment inside a fragment? - android

This is my fragment which is in navigation menu.
class MirandaFragment extends Fragment {
protected View view;
FragmentPagerAdapter adapterViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.miranda, container, false);
ViewPager vpPager = (ViewPager) view.findViewById(R.id.vpPager);
adapterViewPager = new MyPageAdapter(getFragmentManager());
vpPager.setAdapter(adapterViewPager);
return view;
}
}
This is the adapter where I has set two fragments EngMiranda and SpaMiranda.
class MyPageAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;
public MyPageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages.
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for a particular page.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return EngMiranda.newInstance("Fragment 1", getCount());
case 1:
return SpaMiranda.newInstance("Fragment 2", getCount());
default:
return null;
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "English";
case 1:
return "Spanish";
default:
return null;
}
}
}
After app gets open for first time then I am able to open the fragment and view the tab fragments in it but when we open that fragment for second time then adapter is not displaying.

Related

SectionsPageAdapter with FragmentPageAdapter

First let me try to reproduce my Problem/Question.
My Activity is a TabbedActivity with 3 Tabs, done with a SectionPageAdapter and a FragmentPageAdapter.
On these 3 Tabs the user is input Data. With just swiping the user can change the tab. After the User has filled in all data, he must click on a FloatingActionButton to save the data.
Now there is my Problem i donĀ“t know if i can access these 3 Fragments with the filled in data ?
Can i load all 3 Fragments with the filled in Data, read the data and save it or must i handle the PageChangeListenEvent and saved the Data before i change the Page?
Code:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TravelInfoActivity.PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return maxTabs;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_FRAGMENT_ID = "fragment_id";
private static int REQUEST_CODE_PLACE = 1;
private View currentRootView;
private View currentClickedView;
#SuppressLint("UseSparseArrays")
private HashMap<Integer, View> hMap = new HashMap<>();
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static TravelInfoActivity.PlaceholderFragment newInstance(int sectionNumber) {
TravelInfoActivity.PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
int fragmentLayout;
switch (sectionNumber) {
case 1:
fragmentLayout = R.layout.fragment_accommodation;
break;
case 2:
fragmentLayout = R.layout.fragment_visited_places;
break;
case 3:
fragmentLayout = R.layout.fragment_additional_info;
break;
default:
fragmentLayout = R.layout.fragment_accommodation;
}
args.putInt(ARG_FRAGMENT_ID, fragmentLayout);
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(getArguments().getInt(ARG_FRAGMENT_ID), container, false);
currentRootView = rootView;
switch (getArguments().getInt(ARG_FRAGMENT_ID)) {
// switch handling here, not relevant
}
return rootView;
}
Guys i have done it refreshed my brain ;)
Just save the instance from the Fragment to an array and you can use it later.
I made this in the SectionPageAdapter and ovverided the mehtod instantiateItem().
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
Integer fragmentId = fragment.getArguments().getInt("fragment_id");
registeredFragments.put(fragmentToPageMap.get(fragmentId), fragment);
return fragment;
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
public boolean registeredFragmentExists(int position) {
if(registeredFragments.size() >= position) {
if(registeredFragments.get(position) != null) {
return true;
}
}
return false;
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return TravelInfoActivity.PlaceholderFragment.newInstance(position);
}
#Override
public int getCount() {
return maxTabs;
}
}
Later you can access the fragments rootView with
mSectionsPagerAdapter.getRegisteredFragment(position);
fragment.getActivity().findViewById(id);

6 Fragments hided/shown by ViewPager without being dismissed

I have 6 Fragments within an activity that I created as
I want to swipe from one to the other and need them to not being dismissed but onyl hide/show y the viewpager.
Is there a way to derive a viewpager class or a SectionsPagerAdapter to do that.
I already add
mViewPager.setOffscreenPageLimit(6);
but it didn't work. When swiping from the 4th fragment to 3rd or to 5th, the app crashes.
Relevant parts of my code simpified and renamed Fragment classes to get the code readable:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the fragment once for all !
if (savedInstanceState == null) {
fragment1 = Fragment1.newInstance(1);
fragment2 = Fragment2.newInstance(2);
fragment3 = Fragment3.newInstance(3);
fragment4 = Fragment4.newInstance(4);
fragment5 = Fragment5.newInstance(5);
fragment6 = Fragment6.newInstance(6);
}
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(6);
});
public static class Fragment1 extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public Fragment1() {
}
public static Fragment1 newInstance(int sectionNumber) {
Fragment1fragment = new Fragment1();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
fragment.setRetainInstance( true);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
// "Main Frag"
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
#Override
public void onResume() {
super.onResume();
// The activity is about to become visible again, recalcule
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//return PlaceholderFragment.newInstance(position + 1);
switch (position)
{
case 0 : return fragment1; //instead of Fragment1.newInstance(1);
case 1 : return fragment2;
case 2 : return fragment3;
case 3 : return fragment4;
case 4 : return fragment5;
case 5 : return fragment6;
default: return null;
}
}
#Override
public int getCount() {
// Show 6 total pages.
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
case 5:
return "SECTION 6";
}
return null;
}
}
Extend your section pager adapter by FragmentSatePagerAdaper
And set
mViewPager.setOffscreenPageLimit(6);

How to implement swipe view on fragment

I'm trying to implement a swipe inside a fragment! My application use a Navigation Drawer Layout and I change view with the drawer. All the view that I show are fragment. Now, I want to implement into a specific fragment an horizontal swipe. How can I do this thing? my code is here :
public class MybookingFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
final View Rootview = inflater.inflate(R.layout.mybooking, parent, false);
ViewPager viewPager = (ViewPager) Rootview.findViewById(R.id.viewpager);
FragmentManager fragmentManager = getChildFragmentManager();
viewPager.setAdapter(new SlidingPagerAdapter(fragmentManager));
return Rootview;
}
my adapter code is here
public class SlidingPagerAdapter extends FragmentStatePagerAdapter {
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
public SlidingPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Class fragmentClass = null;
switch (position) {
case 0:
fragmentClass = UpcomingOrder.class;
case 1:
fragmentClass = RunningOrder.class;
break;
case 2:
fragmentClass = CompletedOrder.class;
break;
default:
fragmentClass = UpcomingOrder.class;
break;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return fragment;
}
#Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
Try this below example. I think you are asking this only
Example 1
Example 2
Tabs swipe
You have to use main fragment. with in that fragment you have to load all sub-fragments(tabs).

SwipeView and FragmentPageAdapter

Right now i'm trying to program an android-application with a navigation drawer and swipe views. When selecting "Timetable" in the navigation drawer, the app opens a swipe view with 6 pages with the page number on every page.
The problem is that - when re-selecting the page "Timetable" in the navigation drawer - the pages 5 and 6 are displayed but the content (fragment) won't load.
I used the code of the swipe view-example: http://developer.android.com/training/implementing-navigation/lateral.html
This should be the fragment with the swipe views:
public class Timetable extends Fragment {
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
public TimeTable() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.screen_stundenplantag,
container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(this.getActivity()
.getSupportFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
Here's the class SectionsPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 6 total pages.
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
case 5:
return getString(R.string.title_section6).toUpperCase(l);
}
return null;
}
}
And last but not least here's the DummySectionFragment-class:
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static String ARG_SECTION_NUMBER = "";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.append("Page "
+ Integer.toString(getArguments()
.getInt(ARG_SECTION_NUMBER)));
Log.i(this.getClass().getSimpleName(),String.valueOf(getArguments().get(DummySectionFragment.ARG_SECTION_NUMBER)));
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
return rootView;
}
}

FragmentPagerAdapter class is triggered only when the tab is clicked for the first time

I'm trying to develop a tab based application. Each tab has its fragment. One of them has ViewPager, and ViewPager has its own pages. I move from to another by scrolling in this viewpager.
When I open the application for the first time, there is no problem. However, when I move from this tab, and come to it again, it does not show viewpager content, it does not fire FragmentPagerAdapter class.
Tab A - Tab B - Tab C
Tab C Fragment has ViewPage.
ViewPage has its own pages.
public class FragmentC extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
static ViewPager C_mViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
C_mViewPager = (ViewPager)inflater.inflate(R.layout.fragmentC, container,false);
C_SectionsPagerAdapter mC_PagerAdapter=new C_SectionsPagerAdapter(MainActivity.fragmentManagerv4);
C_mViewPager.setAdapter(mC_PagerAdapter);
return C_mViewPager;
}
public class C_SectionsPagerAdapter extends FragmentPagerAdapter {
public C_SectionsPagerAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null ;
Bundle args;
switch (i) {
case 0:
fragment=new A_SubFragment();
break;
case 1:
fragment = new B_SubFragment();
break;
case 2:
fragment = new C_SubFragment();
break;
case 3:
fragment = new D_SubFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
public static class A_SubFragment extends Fragment {
public A_SubFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout theLayout=(RelativeLayout)inflater.inflate(R.layout.a_sub_fragment, container,false);
//...
return theLayout;}}
}
Fragment C;
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
I got the answer, I used FragmentStatePagerAdapter Instead of FragmentPagerAdapter

Categories

Resources