gpt4 book ai didi

java - 重新加载Fragment,Fragment不调用onCreateView

转载 作者:行者123 更新时间:2023-12-02 02:37:19 25 4
gpt4 key购买 nike

我有一个 fragment (C)和一个带有2个 fragment (A/B)的ViewPager(适配器)。当我调用一次 fragment 时,它们加载得很好。但是当我第二次调用 Fragment(C) 时,Fragment A 和 B 不会调用 View onCreateView 方法,并且不会加载。

使用 Fragmetmanger 调用 fragment C:

FragmentC fragment = new FragmentC();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.adressenmdf_detail_container, fragment).addToBackStack(null).commit();

fragment C

 public BelegeDetails(){

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_belege_details, container, false);
//TODO Actionbar noch anpassen
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);



viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();

fragment C 的设置

    private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(this.getFragmentManager());

adapter.addFragment(new BelegeDetails1(),"Details" );
adapter.addFragment(new BelegeDetailsPos(), "Positionen");
viewPager.setAdapter(adapter);
}
private void setupTabIcons() {
tabLayout.getTabAt(1).setIcon(R.drawable.ic_belege);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

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

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);

}

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

fragment A BelegDetails1 和 fragment B BelegDetailsPos 是正常的 fragment 类,例如:

  public BelegeDetails1() {
// Required empty public constructor

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState.......

最佳答案

尝试改变这个:

ViewPagerAdapter adapter = new ViewPagerAdapter(this.getFragmentManager());

与:

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

当您嵌套 fragment 时,您需要使用 getChildFragmentManager() 而不是 getFragmentManager()

关于java - 重新加载Fragment,Fragment不调用onCreateView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46095391/

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