gpt4 book ai didi

android - Fragment 中的 ViewPager 在返回时丢失内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:18 33 4
gpt4 key购买 nike

我正在使用 FragmentActivity 实现 tabhost,在第一个选项卡下我加载了一个包含 ViewpagerCirclePageIndIcator 的欢迎 fragment ,我需要在 WelcomeFragment 中加载 4 个 View

这是我的欢迎 fragment

public class WelcomeFragment extends Fragment
{
PageAdapter mAdapter;
ViewPager mPager;
CirclePageIndicator mIndicator;
List<Fragment> fragments;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_welcome_layout,null);
mPager=(ViewPager)view.findViewById(R.id.myViewPager);
mIndicator=(CirclePageIndicator)view.findViewById(R.id.circlePageIndicator1);
fragments=new ArrayList<Fragment>();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Fragment fragment;
for(int i=0;i<4;i++)
{
fragment= Fragment.instantiate(getActivity(), TipsFragment.class.getName());
fragments.add(fragment);
}
mAdapter = new PageAdapter(getFragmentManager(),fragments);
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
}
}

这是我的页面适配器

public class PageAdapter extends FragmentPagerAdapter implements Serializable{

List<Fragment> fragments;
public PageAdapter(FragmentManager fm,List<Fragment> frags) {
super(fm);
fragments = frags;

}

@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return TipsFragment.newInstance(0, 0);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();

super.destroyItem(container, position, object);
}

}

当我首先加载应用程序时,tabFragmentActivity 加载成功并且 welcomeFragment 中的 viewpager 工作正常,但是当我移动到第二个选项卡并再次返回到第一个选项卡时,ViewPager 没有加载成功地。

我该如何解决?

根据建议我使用了 getChildFragmentManager 但应用程序崩溃了logcat 错误

09-09 13:28:39.412: E/AndroidRuntime(3100): FATAL EXCEPTION: main
09-09 13:28:39.412: E/AndroidRuntime(3100): java.lang.IllegalStateException: No activity
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Handler.handleCallback(Handler.java:725)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Looper.loop(Looper.java:137)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-09 13:28:39.412: E/AndroidRuntime(3100): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 13:28:39.412: E/AndroidRuntime(3100): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 13:28:39.412: E/AndroidRuntime(3100): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 13:28:39.412: E/AndroidRuntime(3100): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-09 13:28:39.412: E/AndroidRuntime(3100): at dalvik.system.NativeStart.main(Native Method

)

最佳答案

如果你在 Fragment 中使用你的 ViewPager,当你创建一个新的适配器时,你应该使用 ChildFragmentManager:

getChildFragmentManager()

关于android - Fragment 中的 ViewPager 在返回时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18698557/

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