Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment in ViewPager using FragmentPagerAdapter is blank the second time it is viewed

I have a fragment interface with tabs along the bottom which open different fragments in the main view.

I have one particular fragment which is a list of items. If the user selects one of the items in this list, another fragment opens which contains a viewpager which scrolls horizontally between all of the items in the list in the previous fragment. This works great.

The viewpager uses a FragmentPagerAdapter to display the items.

The problem comes when the user selects an item in the list, views it, then hits the button on the tab bar to go back to the list, then selects another item. The second time an item is selected, a blank screen appears instead of the viewpager. I receive no errors in my LogCat when this happens.

Why is the viewpager only appearing the first time?

FragmentPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {     Cursor mCursor;      public ViewPagerAdapter(FragmentManager fm, Cursor c) {         super(fm);         mCursor = c;     }      public void changeCursor(Cursor c) {         mCursor = c;         this.notifyDataSetChanged();     }      @Override     public int getCount() {         if (mCursor == null) return 0;         else return mCursor.getCount();     }      @Override     public Fragment getItem(int position) {         mCursor.moveToPosition(position);         return TeamCardFragment.newInstance(mCursor, position);     } } 

PagerFragment:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     Bundle bundle = getArguments();     mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);      View mView = inflater.inflate(R.layout.team_card_master, container, false);     mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);      mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);     new setAdapterTask().execute();      return mView; }  private class setAdapterTask extends AsyncTask<Void, Void, Void> {     protected Void doInBackground(Void... params) {         return null;     }      @Override     protected void onPostExecute(Void result) {         mViewPager.setAdapter(mAdapter);         mViewPager.setCurrentItem(mCursorPosition);     } } 
like image 767
howettl Avatar asked Oct 12 '11 21:10

howettl


People also ask

How do you make a ViewPager only resume selected fragment?

1 Answer. Show activity on this post. You cannot do that, the viewpager requires at least one fragment to the left and one to the right. I suggest you move the onResume() logic to a separate method and call it when the fragment becomes visible.

What is ViewPager used for?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

How do I use Tablayout with ViewPager?

Tab layout are visible below toolbar with View pager, used to create swipeable views . Tabs are designed to work with fragments. Use them to swipe fragments in view pager.


2 Answers

I had the same issue. Changing the parent class of my PageAdapter from android.support.v4.app.FragmentPagerAdapter to android.support.v4.app.FragmentStatePagerAdapter solve my ViewPager display issue on "second time"!

like image 142
Yann Avatar answered Oct 11 '22 20:10

Yann


I managed to solve this by replacing getFragmentManager() with getChildFragmentManager() in the parent fragment. This parent fragment was instancing an android.support.v4.app.FragmentPagerAdapter in order to contain pageable (slideable) fragments, which requires a fragment manager in the constructor. To this constructor I passed the return value of getChildFragmentManager().

hackbod's link was key (https://developer.android.com/about/versions/android-4.2.html#NestedFragments), which was found in this post Fragments within Fragments

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions.

like image 31
Daniel F Avatar answered Oct 11 '22 20:10

Daniel F