Android静态和动态注册Fragment

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43219615/article/details/99674080

Fragment一般只占据一小块地方(比如下面的广告),但是它有自己的生命周期(可以试试重写那些方法来观察一下),下面是静态注册和动态注册使用Fragment的例子。
Fragment

  1. 创建自己的Fragment,如MyFragment。布局和代码如下。
<?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. 静态注册。直接在活动的布局文件中加fragment节点即可,注意name属性必须填写完整的路径,下面是一个例子的布局文件和代码。
<?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. 动态注册。下面是代码和详细注释(注意这只是一个例子,如果这样写的话每次onCreate时都会创建一个Fragment(如横屏向竖屏的转换))。
<?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();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43219615/article/details/99674080