Android static and dynamic registration Fragment

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99674080

Fragment generally only occupy a small area (such as ad below), but it has its own life cycle (you can try to rewrite those methods to look at), the following is an example of using Fragment static registration and dynamic registration.
Fragment

  1. Create your own Fragment, such as MyFragment. And layout code is as follows.
<?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:layout_height="wrap_content">

    <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:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/iv_adv"
        android:layout_weight="5"
        android:src="@drawable/adv"
        android:scaleType="fitCenter"/>
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MyFragment extends Fragment {
    protected View view;
    protected Context context;
    private TextView tv_adv;
    private ImageView iv_adv;

    @Nullable
    @Override
    //创建Fragment视图
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = this.getActivity();
        //从xml文件获取对象 以便代码操作
        view = inflater.inflate(R.layout.my_fragment, container, false);
        tv_adv = view.findViewById(R.id.tv_adv);
        tv_adv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "你点击了广告的文本", Toast.LENGTH_SHORT).show();
            }
        });
        iv_adv = view.findViewById(R.id.iv_adv);
        iv_adv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "你点击了广告的图片", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
  1. Static registration. Directly added to the active node can fragment layout file, note the name attribute must fill in the full path, the following is an example of the layout file and code.
<?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"
    tools:context=".StaticFragmentActivity"
    android:orientation="vertical"
    android:padding="5dp">

    <!--每个fragment必须指定id属性 name属性要填写完整的路径-->
    <fragment
        android:id="@+id/fragment_static_demo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="xyz.strasae.androidlearn.myandroidapplication.MyFragment"/>
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class StaticFragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static_fragment);
    }
}
  1. Dynamic registration. Here is the code and detailed comments (Note that this is only an example, if you write it so onCreate is created every time a Fragment (such as conversion to a horizontal screen vertical screen))。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_dynamic_fragment_layout"
    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"
    tools:context=".DynamicFragmentActivity"
    android:orientation="vertical">
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class DynamicFragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_fragment);
        //获取MyFragment实例
        MyFragment myFragment  = new MyFragment();
        //获取FragmentManager实例
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        //开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //通过事务向布局文件中添加碎片
        fragmentTransaction.add(R.id.ll_dynamic_fragment_layout, myFragment);
        //提交事务
        fragmentTransaction.commit();
    }
}

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99674080