Fragment
静态注册
- 主布局、Fragment布局、主函数、Fragment类
主布局
将Fregment布局声明出来
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_static"
android:name="com.example.fragment.StaticFragment"
android:layout_width="match_parent"
android:layout_height="60dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是每个界面的具体内容!"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
Fragment布局
主布局中声明的碎片,也就是Fragment内部的布局方式。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#bbffbb">
<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="#000000"
android:textSize="17sp" />
<ImageView
android:id="@+id/iv_adv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:src="@drawable/adv"
android:scaleType="fitCenter" />
</LinearLayout>
主函数
将Fragment实例化出来。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment类
返回Fregment对应的视图对象
public class StaticFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "StaticFragment";
protected View mView; // 声明一个视图对象
protected Context mContext; // 声明一个上下文对象
// 创建碎片视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// mContext = getActivity(); // 获取活动页面的上下文
// // 根据布局文件fragment_static.xml生成视图对象
// 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; // 返回该碎片的视图对象
return inflater.inflate(R.layout.fragment_static, container, false); // 返回该碎片的视图对象
}
生命周期
public class StaticFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "fragment";
protected View mView; // 声明一个视图对象
protected Context mContext; // 声明一个上下文对象
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.e(TAG, "onAttach...");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate...");
}
// 创建碎片视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView...");
// mContext = getActivity(); // 获取活动页面的上下文
// // 根据布局文件fragment_static.xml生成视图对象
// 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; // 返回该碎片的视图对象
return inflater.inflate(R.layout.fragment_static, container, false); // 返回该碎片的视图对象
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onViewCreated....");
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onStart() {
Log.e(TAG, "onStart...");
super.onStart();
}
@Override
public void onResume() {
Log.e(TAG, "onResume...");
super.onResume();
}
@Override
public void onClick(View v) {
}
}
2022-07-03 19:41:41.817 1809-1809/com.example.fragment E/fragment: onAttach...
2022-07-03 19:41:41.818 1809-1809/com.example.fragment E/fragment: onCreate...
2022-07-03 19:41:41.818 1809-1809/com.example.fragment E/fragment: onCreateView...
2022-07-03 19:41:41.825 1809-1809/com.example.fragment E/fragment: onViewCreated....
2022-07-03 19:41:41.832 1809-1809/com.example.fragment E/fragment: onStart...
2022-07-03 19:41:41.836 1809-1809/com.example.fragment E/fragment: onResume...