• Fragment使用总结


    • 一个Fragment不能脱离Activity而存在
    • Activity是屏幕的主体,而Fragment是Activity的一个组成元素

    静态加载

    MainActivity

    package com.example.fragmentdemo;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //静态加载frament
                    startActivity(new Intent(MainActivity.this,staticloadFragmentActivity.class));
                }
            });
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    activitymain.xml

    
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView
            android:id="@+id/textView"
            android:gravity="center"
            android:text="static load fragment"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            />
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ListFrament

    package com.example.fragmentdemo;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    //列表fragment
    public class ListFrament extends Fragment {
        @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    
        //创建视图,主要方法
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_list, container, false);
            return view;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    fragment_list.xml

    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#E46CC6"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView2"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:text="TextView" />
    RelativeLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    staticloadFragmentActivity

    package com.example.fragmentdemo;
    
    import android.os.Bundle;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class staticloadFragmentActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_static_load_fragment);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    activity_static_load_fragment.xml

    
    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <fragment
            android:layout_centerInParent="true"
            android:id="@+id/listFrament"
            android:name="com.example.fragmentdemo.ListFrament"
            android:layout_width="300dp"
            android:layout_height="200dp"/>
    
    RelativeLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    主线程点击textview触发

    startActivity(new Intent(MainActivity.this,staticloadFragmentActivity.class));
    
    • 1

    跳到staticloadFragmentActivity中,然后

    setContentView(R.layout.activity_static_load_fragment);
    
    • 1

    加载视图activity_static_load_fragment

    <fragment
        android:layout_centerInParent="true"
        android:id="@+id/listFrament"
        android:name="com.example.fragmentdemo.ListFrament"
        android:layout_width="300dp"
        android:layout_height="200dp"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    该视图中fragment又绑定了 android:name=“com.example.fragmentdemo.ListFrament”

    也就是ListFrament类

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    中又加载了视图layout.fragment_list

    感觉上边过于繁琐于是修改代码

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_static_load_fragment);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    直接指向fragment,fragment加载两个不同的视图

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <fragment
            android:layout_centerInParent="true"
            android:id="@+id/listFrament"
            android:name="com.example.fragmentdemo.ListFrament"
            android:layout_width="300dp"
            android:layout_height="200dp"/>
        <fragment
            android:id="@+id/listFrament2"
            android:name="com.example.fragmentdemo.ListFrament2"
            android:layout_width="300dp"
            android:layout_height="200dp"/>
    
    </RelativeLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    只需要定义视图加载的类,就可以自定义Fragment了

    特点:

    模块化 可重用 可适配屏幕尺寸 必须再activity中

    动态加载

    activity_main.xml

    
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView
            android:id="@+id/textView"
            android:gravity="center"
            android:text="static load fragment"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            />
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:layout_margin="5dp"
                    android:orientation="horizontal"
                    android:id="@+id/listContainer"
                    android:layout_width="150dp"
                    android:layout_height="match_parent">
                    
                LinearLayout>     
                <LinearLayout
                    android:layout_margin="5dp"
                    android:orientation="horizontal"
                    android:id="@+id/detailContainer"
                    android:layout_width="200dp"
                    android:layout_height="match_parent">
                LinearLayout>
            LinearLayout>
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    MainActivity

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListFrament frament = new ListFrament();
            ListFrament2 frament1 = new ListFrament2();
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.listContainer,frament)
                    .commit();
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.detailContainer,frament1)
                    .commit();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    一个fragment只能被动态加载一次

    好处:

    灵活 可以加判断语句等操作

    传值

    Activity向Fragment传值

    1.setArguments

    在ListFragment中添加静态方法

    public static final String Boundler_TITLE = "title";
    private String title = "Love";
    
    public static ListFrament newInstance(String title){
        ListFrament frament = new ListFrament();
        Bundle bundle=new Bundle();
        bundle.putString(Boundler_TITLE,title);
        frament.setArguments(bundle);
    
        return frament;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在oncreate中获取传入的值

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            title = getArguments().getString(Boundler_TITLE);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建视图时使用这个值

    //创建视图,主要方法
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        TextView textview = view.findViewById(R.id.textView2);
        textview.setText(title);
        return view;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    MainActivity调用

    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.listContainer,new ListFrament().newInstance("ABCD"))
            .commit();
    
    • 1
    • 2
    • 3
    • 4

    Fragment向Activity传值

    使用回调

    在ListFragment中创建接口

    //2.定义全局变量
    OnTitleClickListener onTitleClickListener;
    
    //3.设置接口的方法
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
        this.onTitleClickListener = onTitleClickListener;
    }
    
    //1.定义接口
    public interface OnTitleClickListener{
        void onClick(String title);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    设置Fragment被点击的时候,使用回调函数

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        TextView textview = view.findViewById(R.id.textView2);
        textview.setText(title);
        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onTitleClickListener!=null){
                    onTitleClickListener.onClick(title);
                }
            }
        });
        return view;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在MainActivity中实现这个接口

    public class MainActivity extends AppCompatActivity implements ListFrament.OnTitleClickListener {
    
        public  ListFrament listFrament = new ListFrament().newInstance("ABCD");
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ListFrament2 frament1 = new ListFrament2();
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.listContainer, listFrament)
                    .commit();
            listFrament.setOnTitleClickListener(this);
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.detailContainer,frament1)
                    .commit();
        }
    
        @Override
        public void onClick(String title) {
            setTitle(title);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    点击fragment就会实现回调了

    扩展 传对象

    private User user;
    public class User{
    
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    public static ListFrament newInstance(String title,User user){
        ListFrament frament = new ListFrament();
        Bundle bundle=new Bundle();
        bundle.putString(Boundler_TITLE,title);
        frament.setArguments(bundle);
        frament.setUser(user);
        return frament;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    直接添加这个对象的set方法再调用即可

    Fragment和Fragment之间传值,之间调用回调即可

  • 相关阅读:
    管理会计习题集及答案 5-7章
    LFMCW雷达的距离分辨率
    CLR C#--线程基础
    特征值和特征向量简单入门
    数据结构与算法之贪心&动态规划
    show processlist 详解
    ArangoDB关键知识点汇总大全(不定时更新)
    Kotlin协程:MutableSharedFlow的实现原理
    设计模式之组合模式
    五万字 | 深入理解Linux内存管理
  • 原文地址:https://blog.csdn.net/liuwushuang233/article/details/126409975