Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

android - ViewPager in TabFragment is not loading a second time

I'm trying to make an app that has a ViewPager in a Fragment that is part of a TabHost. Everything works out fine. I have my tabbar, I can switch tabs. When I switch to the tab with the ViewPager, all is shown correctly.

However, as soon as I leave this tab with the ViewPager and return this tab, my content is not shown. If I scroll to the side twice I do see my next image and if I go back two times I also see the images are loaded (probably the offscreenloaded).

See that my TabFragment is being reinstantiated when I return to it but the fragments in the ViewPager aren't.

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    mProjectText = (TextView) getView().findViewById(R.id.projectText);
    mProjectText.setText(mActiveProject.getInspirationText());

    mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());

    mPager = (ViewPager)getView().findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    super.onActivityCreated(savedInstanceState);
}

public class AlbumAdapter extends FragmentStatePagerAdapter {

private ArrayList<ProjectContent> mItems;

public AlbumAdapter(FragmentManager fm, ArrayList<ProjectContent> items) {
    super(fm);
    this.mItems = items;
}

@Override
public Fragment getItem(int position) {
    return AlbumContentFragment.newInstance(mItems.get(position));
}

@Override
public int getCount() {
    return mItems.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}}

The mockup for the app

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the problem. It took me two days, but hey, it's fixed.

Instead of using

mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());

You should use

mAdapter = new AlbumAdapter(getChildFragmentManager(), mActiveProject.getInspiration());

So much for 5 characters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...