ViewPager + FragmentPagerAdapter +每个标签多个片段

时间:2012-10-09 17:12:45

标签: android android-fragments android-support-library

首先,我引用了其他一些讨论这个问题的线程,如下所示: Replace Fragment inside a ViewPager

我有一个自定义FragmentStackAdapter来管理我的应用程序的所有片段,客户端的最终请求是显示所有应用程序中的选项卡,所以我决定实现SherlockActionBar + tabs + viewpager(用于幻灯片)。

我有FragmentStackAdapter的下一个实现:

public class FragmentStackAdapter extends FragmentPagerAdapter {

/**
 * Logging tag
 */
private final static String TAG = "FragmentStackAdapter";

/**
 * Fragment Manager
 */
private FragmentManager fm;

/**
 * Container of this adapter
 */
private ViewPager pager;

/**
 * Current tab position (SET THIS in the TAB LISTENER)
 */
private Integer currentTabPosition;

/**
 * Array of stacks to get fragment stack list.
 */
private SparseArray<LinkedList<Fragment>> stackFragments = new SparseArray<LinkedList<Fragment>>();

/**
 * Default constructor
 * @param fm FragmentManager
 */
public FragmentStackAdapter(FragmentManager fm, ViewPager pager) {

    super(fm);
    this.fm = fm;
    this.pager = pager;
}

public void addRootFragmentAtPosition(int tabPos, Fragment rootFragment) {

    if (tabPos > stackFragments.size() - 1) {
        stackFragments.put(tabPos, new LinkedList<Fragment>());
    }

    LinkedList<Fragment> stack = stackFragments.get(tabPos);
    stack.add(0, rootFragment);

}

public Fragment getRootFragment(int tabPos) {

    return stackFragments.get(tabPos).get(0);

}

public void addFragment(Fragment f) {

    startUpdate(pager);

    Tools.logLine(TAG, "addFragment(): Fragment:" + f + ", in position: " + currentTabPosition);
    LinkedList<Fragment> stack = stackFragments.get(currentTabPosition);
    Fragment last = stack.getLast();
    stack.addLast(f);

    fm.beginTransaction()
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .remove(last)
        .add(pager.getId(), f)
        .commit();


    finishUpdate(pager);
    notifyDataSetChanged();

}

/**
 * Goes back in the current selected tab.
 * @return true if it goes back, false if cannot cause the stack is in the last element. Util for handle super.onPressback().
 */
public boolean back() {

    LinkedList<Fragment> stack = stackFragments.get(currentTabPosition);

    if (stack.size() > 1) {

        startUpdate(pager);

        Fragment currentFragment = stack.getLast();
        stack.removeLast();
        fm.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .remove(currentFragment)
            .add(pager.getId(), stack.getLast())
        .commit();

        notifyDataSetChanged();
        finishUpdate(pager);

        return true;

    }

    return false;
}

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

@Override
public Fragment getItem(int pos) {

    LinkedList<Fragment> stack = stackFragments.get(pos);

    Fragment last = stack.getLast();

    Tools.logLine(TAG, "getItem() pos: " + pos + ", fragment:" + last.toString() + ", isV:" + last.isVisible() + ", isD:" + last.isDetached());
    return last;

}

@Override
public int getItemPosition(Object object) {

    int size = stackFragments.size();
    for (int i = 0 ; i < size ; i++) {

        int key = stackFragments.keyAt(i);
        LinkedList<Fragment> stack = stackFragments.get(key);

        if (stack.contains(object)) {

            int indexOf = stack.indexOf(object);

            // is root activity and is the current selected tab
            if (key == currentTabPosition && indexOf == 0) {

                return POSITION_NONE; // force reload this fragment

            } else if (indexOf == 0) { // if root and other, no changes

                return POSITION_UNCHANGED;

            }

            Tools.logLine(TAG, "getItemPos:, indexOf: " + indexOf + ", Object:" + object.toString());
            return indexOf; // return the pos of the activity

        }

    }

    Tools.logLine(TAG, "getItemPos: RETURN DEFAULTTTTT POSITION_NONE, " + object.toString()); 

    return POSITION_NONE; // the current item is not in the stack, so probabilly we removed it pressing back. Reload this tab will get another getItem and return the new corresponding item

}

@Override
public CharSequence getPageTitle(int position) {

    return "Near";

}

@Override
public long getItemId(int position) {

    LinkedList<Fragment> stack = stackFragments.get(position);
    Fragment f = stack.getLast();

    int rtnid = f.hashCode();

    Tools.logLine(TAG, "getItemId() for pos: " + position + ", Fragment: "+ f  +", rtnId: " + rtnid + ", hex: " + Integer.toString(rtnid, 16));

    return rtnid;

}

public void setCurrentTabPostion(int currentTabPosition) {
    this.currentTabPosition = currentTabPosition;
}
    }

这就是我从MainActivity初始化的方式:

  

//分配ui fieds           viewPager =(ViewPager)findViewById(R.id.pager);

    // Configure the StackAdapter
    fragmentAdapter = new FragmentStackAdapter(getSupportFragmentManager(), viewPager);
    fragmentAdapter.addRootFragmentAtPosition(TIMELINE_TAG, new TimelineActivity());
    fragmentAdapter.addRootFragmentAtPosition(NEAR_TAG, new NearFragment());

我认为当我调用fragmentAdapter.addFragment()时它工作正常它在先前加载的片段中显示了新片段但是当我处理显示此栈跟踪的“back()”方法时它失败了:

        did=1: thread exiting with uncaught exception (group=0x400205a0)
         FATAL EXCEPTION: main
         java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
            at android.view.ViewGroup.addViewInner(ViewGroup.java:2062)
            at android.view.ViewGroup.addView(ViewGroup.java:1957)
            at android.view.ViewGroup.addView(ViewGroup.java:1914)
            at android.view.ViewGroup.addView(ViewGroup.java:1894)
            at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
            at com.bbva.tweetmeter.ui.FragmentStackAdapter.getItemPosition(FragmentStackAdapter.java:311)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:712)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2519)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.bbva.tweetmeter.ui.FragmentStackAdapter.back(FragmentStackAdapter.java:154)
            at com.bbva.tweetmeter.ui.MainActivity.onBackPressed(MainActivity.java:210)
            at android.app.Activity.onKeyUp(Activity.java:1985)
            at android.view.KeyEvent.dispatch(KeyEvent.java:1513)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2210)
            at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1769)
            at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2716)
            at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2688)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1969)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:143)
            at android.app.ActivityThread.main(ActivityThread.java:4293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

为什么显示此跟踪如果我替换上一个片段为什么要附加父级?有办法解决这个问题吗?

随意问我是否没有正确解释

3 个答案:

答案 0 :(得分:4)

我使用

时遇到了同样的问题
View res = inflater.inflate(R.layout.fragment_guide_search, container);

在Fragment.onCreateView(...

你必须致电

View res = inflater.inflate(R.layout.fragment_guide_search, container, false);

View res = inflater.inflate(R.layout.fragment_guide_search, null);

关注最后一个选项。要理解究竟发生了什么,请阅读这篇精彩的文章: http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

答案 1 :(得分:1)

这是片段的onCreateView实现的问题。我正在返回同样保存在实例变量中的View,然后当片段重新附加时,它再次调用onCreateView但返回相同的View,因此该视图已经有了父视图。正确的实现是重新创建其他视图,而不是保存在上一个视图的实例变量中。

答案 2 :(得分:1)

使用FragmentPagerAdapter时遇到同样的问题,如果你正在使用FragmentPagerAdapter,那么只需在FragmentPagerAdapter中使用destroyItem方法。

@Override
public void destroyItem(ViewGroup container, int position, Object object) 
{
        FragmentTransaction ft=fm.beginTransaction();
        ft.remove((Fragment) object);
        ft.commit();
}

setOffscreenPageLimit(int limit)消耗更多内存,在某些情况下它不能完美运行。 它在任一侧的保留状态参见说明。 setOffscreenPageLimit(int limit) - 设置在空闲状态下视图层次结构中应保留到当前页面任一侧的页面数。