2

首先,我阅读了一些其他线程谈论这个,像这样: 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)

如果我替换前一个片段为什么会显示此跟踪为什么附加了父级?有什么办法处理这个吗?

随意问我是否解释不正确

4

3 回答 3

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/

于 2013-01-22T16:19:09.603 回答
1

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

于 2012-10-11T06:28:53.457 回答
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) - 设置在空闲状态下应保留到视图层次结构中当前页面任一侧的页面数。

于 2015-01-17T06:27:15.953 回答