标签:blog exp start style images tool ted img ide
1、在主页面(activity引用Fragment )的布局文件中定义FrameLayout ,加载Fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
2、初始化Fragment
private void initFragment() {
// Fragment管理器
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();// 开始事务
// 将帧布局替换为对应的Fragment
transaction
.replace(R.id.fl_content, new ContentFragment(), TAG_CONTENT);
transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
TAG_LEFT_MENU);
transaction.commit();// 提交事务
// fm.findFragmentByTag(TAG_CONTENT);
}
3、定义Fragment基类
public abstract class BaseFragment extends Fragment { public Activity mActivity; // Fragment被创建 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = getActivity();// 获取所在的activity对象 } // 初始化Fragment布局 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = initView(); return view; } // activity创建结束 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initData(); } /** * 初始化布局, 子类必须实现 */ public abstract View initView(); /** * 初始化数据, 子类可以不实现 */ public void initData() { } }
4、定义Fragment基类
标签:blog exp start style images tool ted img ide
原文地址:http://www.cnblogs.com/ecollab/p/6029019.html