重写FragmentTabHost,防止FragmentTabHost切换fragment重新调用onCreateView()


可以发现,在上一篇中的Demo中,每次FragmentTabHost切换fragment时会调用onCreateView()重绘UI。 

简单改造一下FragmentMy代码(FragmentHome和FragmentMessage不给出),每次切换时总会有Toast。

public class FragmentMy extends Fragment {

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

		View view = inflater.inflate(R.layout.fragment_my, null);

		T.showShort(getActivity(), "FragmentMy==onCreateView");

		return view;
	}
}

为了防止FragmentTabHost切换fragment重新调用onCreateView(),可以重写FragmentTabHost

FragmentTransaction有几个常用方法:

attach(Fragment fragment)

Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment).This causes its view hierarchy to be re-created, attached to the UI, and displayed.

detach(Fragment fragment)

DDetach the given fragment from the UI. This is the same state as when it is put on the back stack: 
the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. 
When going into this state its view hierarchy is destroyed.(从UI中移除)

 hide(Fragment fragment)

扫描二维码关注公众号,回复: 3268774 查看本文章

Hides an existing fragment. 
This is only relevant for fragments whose views have been added to a container,
 as this will cause the view to be hidden.(只是隐藏,不会销毁)

 show(Fragment fragment)

Shows a previously hidden fragment. 
This is only relevant for fragments whose views have been added to a container, as this will cause the view to be shown.(把隐藏的显示出来)

所以只需重写FragmentTabHost,将detach替换为hide,隐藏Fragment,将attach替换为show,显示Fragment就可以了。

private FragmentTransaction doTabChanged(String tabId,
			FragmentTransaction ft) {
		TabInfo newTab = null;
		for (int i = 0; i < mTabs.size(); i++) {
			TabInfo tab = mTabs.get(i);
			if (tab.tag.equals(tabId)) {
				newTab = tab;
			}
		}
		if (newTab == null) {
			throw new IllegalStateException("No tab known for tag " + tabId);
		}
		if (mLastTab != newTab) {
			if (ft == null) {
				ft = mFragmentManager.beginTransaction();
			}
			if (mLastTab != null) {
				if (mLastTab.fragment != null) {
					// 将detach替换为hide,隐藏Fragment
					// ft.detach(mLastTab.fragment);
					ft.hide(mLastTab.fragment);
				}
			}
			if (newTab != null) {
				if (newTab.fragment == null) {
					newTab.fragment = Fragment.instantiate(mContext,
							newTab.clss.getName(), newTab.args);
					ft.add(mContainerId, newTab.fragment, newTab.tag);
				} else {
					// 将attach替换为show,显示Fragment
					// ft.attach(newTab.fragment);
					ft.show(newTab.fragment);
				}
			}

			mLastTab = newTab;
		}
		return ft;
	}

注意在XML中更改命名空间

Demo下载地址:

http://download.csdn.net/detail/yalinfendou/8538703


还有一种在fragment onCreateView 里缓存View,防止每次onCreateView 的时候重绘View

以下代码来自网上:

public class FragmentHome extends Fragment {

	private View rootView;

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

		if (rootView == null) {
			rootView = inflater.inflate(R.layout.fragment_home, null);
			T.showShort(getActivity(), "FragmentHome==onCreateView");
		}

		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}
		return rootView;

	}
}


猜你喜欢

转载自blog.csdn.net/yalinfendou/article/details/44674361