一般创建Fragment需要实现如下三个方法。
onCreate():系统创建Fragment对象后回调该方法,实现代码中只初始化Fragment中的组件。
onCreateView(): 当Fragment绘制界面组件时回调该方法,该方法返回的view就是该Fragment所显示的View。
onPause(): 当用户离开该Fragment时回调该方法。
好了,开始使用吧,最简单的用法——静态使用Fragment
步骤:
1、首先继承Fragment,重写onCreateView()方法设置Fragemnt的布局
2、在Activity中声明此Fragment,然后当做普通的View组件使用
下面一个具体例子:
TitleFragment的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/toolbar_bg"
>
<ImageButton
android:id="@+id/imgBtn_title_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/imgbtn_bg" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Fragment标题"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/toolbar_bg"
>
<ImageButton
android:id="@+id/imgBtn_title_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/imgbtn_bg" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Fragment标题"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
TitleFragment.java类:
public class TitleFragment extends Fragment {
private ImageButton mImageButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title, container, false);
mImageButton = (ImageButton) view.findViewById(R.id.imgBtn_title_fragment);
mImageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "这是TitleFragment! ",
Toast.LENGTH_LONG).show();
}
});
return view;
}
}
同理FirstFragment的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="第一个Fragment"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="第一个Fragment"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
FirstFragment .java类:
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
MainActivity布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment_title_main"
android:name="com.example.activity.TitleFragment"
android:layout_width="match_parent"
android:layout_height="45dp" />
<fragment
android:id="@+id/fragment_first_main"
android:name="com.example.activity.FirstFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fragment_title_main" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment_title_main"
android:name="com.example.activity.TitleFragment"
android:layout_width="match_parent"
android:layout_height="45dp" />
<fragment
android:id="@+id/fragment_first_main"
android:name="com.example.activity.FirstFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fragment_title_main" />
</RelativeLayout>
MainActivity.java类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
说完了静态使用Fragment,下面再说下如何动态使用吧
首先修改下MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment_title_main"
android:name="com.example.activity.TitleFragment"
android:layout_width="match_parent"
android:layout_height="45dp" />
<FrameLayout
android:id="@+id/framelayout_fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearlayout_toolbar_main"
android:layout_below="@+id/fragment_title_main" />
<LinearLayout
android:id="@+id/linearlayout_toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/toolbar_bg" >
<TextView
android:id="@+id/textView1_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="第一个"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="第二个"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment_title_main"
android:name="com.example.activity.TitleFragment"
android:layout_width="match_parent"
android:layout_height="45dp" />
<FrameLayout
android:id="@+id/framelayout_fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearlayout_toolbar_main"
android:layout_below="@+id/fragment_title_main" />
<LinearLayout
android:id="@+id/linearlayout_toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/toolbar_bg" >
<TextView
android:id="@+id/textView1_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="第一个"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="第二个"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
需要使用FragmentManager对Fragment进行动态的加载,这里使用的是replace方法,下一节再详细介绍FragmentManager的常用API。
我们第一节说过Fragment是Android3.0引入的API,所以如果使用以前的版本,需要引入v4的包,然后Activity继承FragmentActivity,然后通过 getSupportFragmentManager获得FragmentManager。所以还是建议把Manifest文件的uses-sdk的 minSdkVersion和targetSdkVersion都改为11以上,这样就不必引入v4包了。
MainActivity.java类
public class MainActivity extends Activity implements OnClickListener {
private Fragment mFirst, mTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView First = (TextView) findViewById(R.id.textView1_main);
TextView Two = (TextView) findViewById(R.id.textView2_main);
First.setOnClickListener(this);
Two.setOnClickListener(this);
// 设置默认的Fragment
setDefaultFragment();
}
private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
mFirst = new FirstFragment();
transaction.replace(R.id.framelayout_fragment_main, mFirst);
transaction.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.textView1_main:
if (mFirst == null) {
mFirst = new FirstFragment();
}
// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.framelayout_fragment_main, mFirst);
break;
case R.id.textView2_main:
if (mTwo == null) {
mTwo = new TwoFragment();
}
transaction.replace(R.id.framelayout_fragment_main, mTwo);
break;
}
// transaction.addToBackStack();
// 事务提交
transaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}