本文介绍了TabFragment中的ViewPager不会再次加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,该应用在

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

推荐答案

我发现了问题。我花了两天时间,但嘿,它固定了。

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

而不是使用

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

您应该使用

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

5个字符太多了。

这篇关于TabFragment中的ViewPager不会再次加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:43