2

我有一个带有一些项目的 FragmentList。如果我单击一个项目,则会打开包含 Viewpager 的相应片段。到目前为止没问题,但是如果我返回列表并再次单击该项目,则不再调用 getItem(int position) 方法。
这里有一个类似问题的链接:
Display fragment viewpager within a fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.activity_fragment_runtime, container, false);
    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    indicator = (TitlePageIndicator) view.findViewById(R.id.indicator);
    MyFragmentPagerAdapter mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager());
    mViewPager.setAdapter(mMyFragmentPagerAdapter);
    indicator.setViewPager(mViewPager);

    return view; 
  }

class MyFragmentPagerAdapter extends FragmentStatePagerAdapter implements TitleProvider {

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public String getTitle(int position) {

        return muxbusTitles[position];
    }

    @Override
    public Fragment getItem(int location) {

        return muxbusFragmentList.get(location);
    }

    @Override
    public int getCount() {

        return muxbusTitles.length;
    }

}

    return view;

}

编辑: 如果有人感兴趣,解决方案是从 PagerAdapter 而不是 FragmentStatePagerAdpater 扩展,因为 Viewpager 已经在 Fragment 中。

4

1 回答 1

0

我确实遇到了类似的问题,在摸索了很长一段时间后,我做出了自己的理解(假设),即即使托管被破坏(未使用 Activity 测试),ViewPager也将其子片段的引用保留在内存中。Fragment or Activity所以我写了这个方法freeFragmentsFromViewPager()。你需要从onDestroy()你的Fragment.

     /**
     * This method free the child fragments from ViewPager as ViewPager keeps the reference
     * of child fragments into memory and next time when you again come to visit view pager it won't show the fragment layout
     * because it has the reference to the fragment whilst the fragment's view was already destroyed.
     */
    private void freeFragmentsFromViewPager()
    {
        for(int i = 0; i < TabsFragmentPagerAdapter.fragments.length; i++)
        {
            Fragment fragment = TabsFragmentPagerAdapter.fragments[i];

            if(fragment != null)
            {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();  
                fragmentTransaction.remove(fragment);
                fragmentTransaction.commit();

                TabsFragmentPagerAdapter.fragments[i] = null;
            }       
        }   
    }

这是我的适配器代码

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter
{
    public static final int NUM_TABS = 3;

    public static Fragment[] fragments = new Fragment[NUM_TABS];

    public TabsFragmentPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        Log.e("TabsFragmentPagerAdapter", "getItem");

        Fragment fragment = TabsFragmentFactory.newInstace(position);

        fragments[position] = fragment;

        return fragment;
    }

    @Override
    public int getCount()
    {
        return NUM_TABS;
    }

    @Override
    public CharSequence getPageTitle(int position) 
    {
        return TabsFragmentFactory.getName(position);
    }
于 2014-11-11T07:18:37.847 回答