gpt4 book ai didi

android - ViewPager + FragmentPagerAdapter + 每个选项卡的多个 fragment

转载 作者:太空狗 更新时间:2023-10-29 16:21:43 24 4
gpt4 key购买 nike

首先我读了一些其他讨论这个话题的帖子,比如: Replace Fragment inside a ViewPager

我有一个自定义的 FragmentStackAdapter 来管理我的应用程序的所有 fragment ,客户端的最终请求是在所有应用程序中显示选项卡,所以我决定实现 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 初始化的方式:

// Assign 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() 时它工作正常它显示了先前加载的 fragment 中的新 fragment 但是当我处理显示此堆栈跟踪的“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)

如果我替换前一个 fragment 为什么会显示此痕迹,为什么要附加父 fragment ?有什么办法可以解决这个问题吗?

如果我解释的不正确,请随意问

最佳答案

我用的时候遇到了同样的问题

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/

关于android - ViewPager + FragmentPagerAdapter + 每个选项卡的多个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com