【问题解决】FragmentTabHost 底部菜单栏,切换Frgment时重新加载问题

public class DisFragment extends BaseFragment {
	private ListView mListView;
	private List mList = new ArrayList();

	private View view;// 缓存Fragment view

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (view == null) {
			// view等于空的时候就加载view 否则跳过此过程,则返回的就是之前的view,注意:加载数据相关的操作就应该在这个位置
			view = inflater.inflate(R.layout.fragment_dis_page, null);
			initTitleBar(inflater, view);
			mListView = (ListView) view.findViewById(R.id.lv_test);
			for (int i = 0; i < 60; i++) {
				mList.add("@ : " + i);
			}
		}
		// 缓存的view需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个view已经有parent的错误。
		ViewGroup parent = (ViewGroup) view.getParent();
		if (parent != null) {
			parent.removeView(view);
		}
		// ******************************************************************************************//
		// 上面代码可以解决如题问题,下面是为了测试用的ListView

		mListView.setAdapter(new CommonAdapter(getActivity(), mList, android.R.layout.simple_list_item_1) {
			@Override
			public void convert(ViewHolder vh, String t) {
				vh.setText(android.R.id.text1, t);
			}
		});
		return view;
	}

	private void initTitleBar(LayoutInflater inflater, View view2) {
		setTitleView(inflater, view);
		setTitleViewVisibility(View.GONE, R.id.back_btn, R.id.right_btn, R.id.right_progress)
				.setTitleViewText(R.id.title, R.string.tab_dis_text);
	}
}


 
  
 
 

你可能感兴趣的:(Android,问题解决)