UI常用组件

UI常用组件,第1张

一、Fragment介绍

1、初识Fragment

  • 设计思想: 为了适配手机、平板等更多设备

 

2、Fragment V Activity

  • Fragment是到Android3.0+以后
  • 一个Activity可以运行多个Fragment
  • Fragment不能脱离Activity而存在
  • Activity是屏幕主体,而Fragment是Activity的一个组成元素

3、生命周期详解

  • onAttach() 在Fragment与Activity发生关联时调用
  • onCreateView() 创建界面创建视图
  • onActivityCreate() 通知Fragment Activity已经创建好,做好下一步准备
  • onDestroyView() 视图被移除时调用
  • onDestroy() 取消关联结束
二、Fragment的加载

静态加载:xml 动态加载:Java code

1、静态加载的应用

activity_main.xml




    


创建Fragment

Fragment1.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

fragment_1.xml




    
    

 

 

2、动态加载的应用

activity_main.xml




    

    

    

    

MainActivity.java

package com.example.fragmentdemo;

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

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // FragmentManager   FragmentTransaction
        //1、获取Fragment管理器
        FragmentManager manager = getSupportFragmentManager();
        //2、获取Fragment事务 (/开启事务)
        FragmentTransaction transaction = manager.beginTransaction();
        //3、动态添加Fragment
        //参数1:容器id
        //参数2:Fragment
        final Fragment f2 = new Fragment2();
        transaction.add(R.id.container, f2);

        //4、提交事务
        transaction.commit();

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1、获取Fragment管理器
                FragmentManager manager = getSupportFragmentManager();
                //2、获取Fragment对象
                Fragment f1 = manager.findFragmentById(R.id.fragment1);
                //3、获取Fragment对象的视图
                View v1 = f1.getView();
                //4、在视图里找到指定的控件
                TextView txt1 = v1.findViewById(R.id.text1);
                //5、修改控件内容
                txt1.setText("这是动态生成内容");

                View v2 = f2.getView();
                //4、在视图里找到指定的控件
                TextView txt2 = v2.findViewById(R.id.text2);
                //5、修改控件内容
                txt2.setText("这是Fragment2效果");

            }
        });
    }
}

fragment_1.xml




    
    

Fragment1.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

fragment_2.xml




    
    

Fragment2.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_2, container, false);
        v.findViewById(R.id.text2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.add(R.id.container, new Fragment3());
                transaction.commit();
            }
        });
        return v;
    }
}

fragment_3.xml




    
    

Fragment3.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Fragment3 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_3, container, false);
    }
}

三、Fragment传值

1、Activity向Fragment

2、Fragment向Activity

3、Fragment之间传值

  坐得住板凳,耐得住寂寞,守得住初心!  

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/719762.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存