如何解决这个问题?不再创造?
ViewPager和FragmentTabHost中的片段生命周期有什么不同?
为什么FragmentShop再次创建?
FragmentShop
public class FragmentShop extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_shop, null);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstShop = (ListView) getView().findViewById(R.id.lstShop);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytShopprogressBar);
btnFloatAndswipeRefresh();
sendAndGetDataWithListener();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
}
FragmentTabHost
FragmentMarket
public class FragmentMarket extends Fragment {
FragmentTabHost mTabHost;
public static Fragment instance() {
Fragment fragment = new FragmentMarket();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_market, container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(R.id.market_tabhost);
mTabHost.setBackgroundResource(R.color.BlueLight);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.market_realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("sales").setIndicator("خرید و فروش"), FragmentSales.class, null);
mTabHost.addTab(mTabHost.newTabSpec("estate").setIndicator("مشاوران املاک"), FragmentEstate.class, null);
mTabHost.addTab(mTabHost.newTabSpec("shop").setIndicator("معرفی بازار"), FragmentShop.class, null);
mTabHost.getTabWidget().getChildAt(0).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(1).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(2).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
关于Fragment instance(): 这个方法只为ViewPager创建页面这里是适配器:
public class AdapterViewPager extends FragmentPagerAdapter {
public AdapterViewPager(Context context, FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "مارکت";
case 1:
return "مجله امروز";
case 2:
return "رویدادها";
case 3:
return "آخرین اخبار";
case 4:
return "دانش آموزان برتر";
case 5:
return "قرعه کشی و جایزه";
case 6:
return "زیبایی و زشتی های شهر";
case 7:
return "حمایت";
default:
return "آخرین اخبار";
}
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentMarket.instance();
case 1:
return FragmentMag.instance();
case 2:
return FragmentEvents.instance();
case 3:
return FragmentNews.instance();
case 4:
return FragmentStudent.instance();
case 5:
return FragmentReward.instance();
case 6:
return FragmentGoodBad.instance();
case 7:
return FragmentSupport.instance();
default:
return FragmentNews.instance();
}
}
@Override
public int getCount() {
return 8;
}
}
答案 0 :(得分:0)
尝试这个。
public FragmentMarket getTestFragment() {
if (marketFragment == null) {
marketFragment = new FragmentMarket ();
}
return marketFragment;
}
答案 1 :(得分:0)
最后解决方案:
这是Fragment LifeCycle: https://developer.android.com/guide/components/fragments.html#Creating
在onDestroyView之后,所有视图都会破坏但不是你的变量
我只是在FragmentShop中这样做
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstSales = (ListView) getView().findViewById(R.id.lstSales);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytSalesprogressBar);
btnFloatAndswipeRefresh();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
if (salesTitle.isEmpty()) {
sendAndGetDataWithListener();
} else {
arrayStructList.clear();
salesDatalistener.onDataRecivce();
}
}
所以你只需要再次创建所有视图并设置参数并且不要获取新数据我只有ListView和浮动按钮
这可以帮助但不适合我:https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en