Fragment in ViewPager using FragmentPagerAdapter i

2020-01-24 10:21发布

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);
    }
}

12条回答
可以哭但决不认输i
2楼-- · 2020-01-24 11:00

for me i had to call this on my viewpager:

myViewPager.setSaveFromParentEnabled(false);

I had the issue where the viewpager was not refreshing and all i saw was a blank white screen where the fragments should be. I was passing in getChildFragmentManager but it did not help.

查看更多
欢心
3楼-- · 2020-01-24 11:04

You can also initialize the adapter for cases where you experience this error when your app is minimized and later called up.

@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);
    initViewPagerAdapter();
    return mView;
}
private void initViewPagerAdapter(){
    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();
}

@Override
public void onResume(){
    super.onResume();
    initViewPagerAdapter();
}
查看更多
Lonely孤独者°
4楼-- · 2020-01-24 11:05

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"!

查看更多
你好瞎i
5楼-- · 2020-01-24 11:05

I had the same issue for which I changed adapter from FragmentPagerAdapter to FragmentStatePagerAdapter and getFragmentManager() in the parent fragment to getChildFragmentManager()

The reason behind this is FragmentStatePagerAdapter is helpful in storing a large number of pages and memory associated with each page visited is less as it keeps only the saved state of the fragment while the page is not visible. This reduces the overhead while switching between the fragments.

查看更多
虎瘦雄心在
6楼-- · 2020-01-24 11:09

We got around this by re-implementing the view pager items as standard views rather than fragments and changing the adapter accordingly.

查看更多
贪生不怕死
7楼-- · 2020-01-24 11:13

This would help you.

    viewPager.setOffscreenPageLimit(position);
查看更多
登录 后发表回答