gpt4 book ai didi

android - FragmentStatePagerAdapter 重复 fragment

转载 作者:行者123 更新时间:2023-11-29 14:14:43 25 4
gpt4 key购买 nike

我在我的单一 Activity 应用程序上使用嵌套的 FragmentStatePagerAdapter。当前布局是:

Activitiy
- Login Fragment
- Master Fragment
- Tab Fragment #1
- Tab Fragment #2
- Tab Fragment #3
- Another Fragment
- Another Fragment

例如,每次我从“另一个 fragment ”切换回“主 fragment ”时,FragmentStatePagerAdapter 都会将同一个项目实例化两次

第一次:Tab fragment #1 #2 #3
第二次:Tab fragment #1 #1 #2 #2 #3 #3
第三次:Tab fragment #1 #1 #1 #2 #2 #2 #3 #3 #3。

我使用的是从 fragment 移动到 fragment

getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, XXXX.newInstance(query), TAG_XXX)
.addToBackStack(null)
.commit();

MasterFragment代码:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initFragmentAdapter();
}

private void initFragmentAdapter() {
final FragmentSimpleAdapter adapter = new FragmentSimpleAdapter(getChildFragmentManager());

adapter.addFragment(
getString(...),
"...",
XXX.class);
...

ViewPager viewPager = (ViewPager) getActivity().findViewById(android.R.id.tabcontent);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(4);

PagerSlidingTabStrip viewTabs = (PagerSlidingTabStrip) getActivity().findViewById(
android.R.id.tabhost);
viewTabs.setViewPager(viewPager);
viewTabs.setOnPageChangeListener(adapter);
}
private final class FragmentSimpleAdapter extends FragmentStatePagerAdapter
implements ViewPager.OnPageChangeListener {
private final List<String> mTitles = new LinkedList<>();
private final List<String> mInstances = new LinkedList<>();
private final List<String> mTag = new LinkedList<>();
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;

public FragmentSimpleAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
mFragmentTags = new HashMap<>();
}

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

@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}

@Override
public Fragment getItem(int position) {
return Fragment.instantiate(getActivity(), mInstances.get(position), null);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
}
return obj;
}

public Fragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null) {
return null;
}
return mFragmentManager.findFragmentByTag(tag);
}

public FragmentSimpleAdapter addFragment(String title, String tag,
Class<? extends Fragment> fragment) {
mTitles.add(title);
mTag.add(tag);
mInstances.add(fragment.getName());
return this;
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
Analytics.sendViewAnalytic(mTag.get(position))
}

@Override
public void onPageScrollStateChanged(int state) {
}
}

最佳答案

问题:

PagerSlidingTabStrip viewTabs = (PagerSlidingTabStrip) getActivity().findViewById(
android.R.id.tabhost);

每次返回 Master's Fragment 时,您都在尝试重新创建 PagerSlidingTabStrip,从而添加了不必要的 fragment 。

解决方法:

你需要先检查选项卡是否已经存在我真的不知道 PagerSlidingTabStrip 我认为这是一个定制类。

关于android - FragmentStatePagerAdapter 重复 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022028/

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