新建一个activity,命名为FragmentStaticActivity,如下
public class FragmentStaticActivity extends Activity {
private static final String TAG = "FragmentStaticActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_static);
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
}
FragmentStaticActivity 对应的布局文件为activity_fragment_static,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<fragment
android:id="@+id/fragment_static"
android:name="com.example.senior.fragment.StaticFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="17sp"
android:gravity="center"
android:textColor="@color/black"
android:text="这里是每个页面的具体内容" />
</LinearLayout>
新建一个java文件命名为StaticFragment,继承于Fragment,如下
public class StaticFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "StaticFragment";
protected View mView;
protected Context mContext;
// 创建碎片视图
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getContext();
mView = inflater.inflate(R.layout.fragment_static, container, false);
TextView tv_adv = mView.findViewById(R.id.tv_adv);
ImageView iv_adv = mView.findViewById(R.id.iv_adv);
tv_adv.setOnClickListener(this);
iv_adv.setOnClickListener(this);
Log.d(TAG, "onCreateView");
return mView;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_adv) {
ToastUtil.showToast(mContext, "您点击了广告文本");
} else if (v.getId() == R.id.iv_adv) {
ToastUtil.showToast(mContext, "您点击了广告图片");
}
}
// 与activity结合。可在该方法中实例化activity的一个回调对象,再Fragment中调用activity的回调方法
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
}
// 在活动页面创建完毕后调用
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
// 回收碎片视图
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
}
// 与activity分离
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
}
StaticFragment 对应的视图文件为fragment_static,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="@color/cyan"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_adv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="广告"
android:textColor="@color/black"
android:textSize="17sp" />
<ImageView
android:id="@+id/iv_adv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:src="@drawable/adv"
android:scaleType="fitCenter" />
</LinearLayout>
效果图如下:
- 点击了图片的效果
- 进入FragmentStaticActivity时的生命周期如左图所示:
- 离开FragmentStaticActivity时的生命周期如右图所示:
注意点:
- xml中fragment节点必须指定id属性,否则运行app会报错,如下:
- 如果页面继承自Activity,StaticFragment类必须继承自android.app.Fragment,否则运行app会报错,如下: