当前位置: 首页 > article >正文

Android : Fragment 传递数据 — 简单应用

示例图:

创建 Fragment  new -> Fragment -> Fragment(Blank)

MainActivity.java

package com.example.fragmentdemo;

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

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_get_result);

        TwoFragment twoFragment = new TwoFragment();
        //数据传递
        Bundle bundle = new Bundle();
        bundle.putString("name","张三丰");
        twoFragment.setArguments(bundle);

        //接收TwoFragment 传递过来的参数
        twoFragment.setPassingData(new TwoFragment.Myinterface() {
            @Override
            public void getResult(String data) {
                textView.setText(data);
            }
        });

        //动态添加Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          // 通过id , 把 twoFragment 添加到LinearLayout布局中
        fragmentTransaction.add(R.id.linear_layout,twoFragment);
        // 替换
//        fragmentTransaction.replace(R.id.linear_layout,twoFragment);
        
        //删除
//        fragmentTransaction.remove(twoFragment);
        // 添加到回退栈  点击返时重新创建fragment  
       fragmentTransaction.addToBackStack(null);
       
        //提交
        fragmentTransaction.commit();
    }
}

HomeFragment.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 HomeFragment extends Fragment {


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

TwoFragment.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;
import android.widget.TextView;


public class TwoFragment extends Fragment {
    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        //获取数据
        String name = getArguments().getString("name");
        textView = view.findViewById(R.id.tv_get_data);
        textView.setText(name);

        return view;
    }

    // 给主页面传递参数
    public void setPassingData(Myinterface myinterface){
        String name = "周芷若";
        myinterface.getResult(name);
    }
    //定义一个接口
    public interface Myinterface{
        void getResult(String data);
    }
}

主布局 activity_main.xml

<?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">


    <TextView
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 简单应用"
        />
    <TextView
        android:id="@+id/tv_get_result"
        android:textSize="24sp"
        android:textColor="#ff00ff00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
 />
    <!-- 注意:要加id
      android:name 引入java类-->
    <fragment
        android:id="@+id/fragment_test"
        android:name="com.example.fragmentdemo.HomeFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        />

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="horizontal" />
</LinearLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#00BCD4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />


</FrameLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff00ff"
    tools:context=".TwoFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment_two" />
    <TextView
        android:text="获取到的数据是:"
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_get_data"
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>


http://www.kler.cn/news/148079.html

相关文章:

  • 入门必读:Figma软件的功能和用途一览!
  • ArcGIS中如何建立土地利用规划数据库
  • 2021秋招-数据结构-栈、队列、数组、列表
  • 测开笔记--Typescript: 文件复制到指定目录
  • 什么手机30万?VERTU唐卡手机顶配56.8万
  • PGP 遇上比特币
  • 【LeeCode】209.长度最小的子数组
  • 大数据平台/大数据技术与原理-实验报告--实战HDFS
  • java stream流map和flatmap的区别
  • 小内存服务器生存指南 ——SWAP 虚拟内存
  • 【GCC】2:chatgpt:SendSideBandwidthEstimation
  • 【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
  • springboot实现数据脱敏
  • 排序算法--快速排序
  • 排序算法:归并排序、快速排序、堆排序
  • QTextEdit 是 Qt 框架中的一个类,用于显示和编辑多行文本内容的可编辑部件
  • 本地开启https,配置nodeJs服务
  • 基于C#实现并查集
  • 华为鸿蒙开发(HarmonyOs开发):超详细的:DevEco Studio 的安装和配置 、华为第三方包依赖:SDK软件包的安装、Nodejs的导入配置
  • 漏洞复现--致远 M3 反序列化 mobile_portal RCE
  • 同旺科技 USB 转 RS-485 适配器 -- 隔离型
  • 钉钉直播不了检查防火墙配置没有拦截应用测试直通都放行的,电脑还可以ping通直播域名,就是开始不了直播
  • Docker Swarm总结+Jenkins安装配置与集成(5/5)
  • Spring代理方式之静态、动态代理(JDK和CGlib动态代理)
  • 解决ansible批量加入新IP涉及known_hosts报错的问题
  • Linux学习笔记6-串口应用
  • OpenJudge NOI 1.8 16:矩阵剪刀石头布 c语言
  • SpringBoot趣探究--1.logo是如何打印出来的
  • 抖音视频如何无水印下载,怎么批量保存主页所有视频没水印?
  • Linux下unzip解压乱码问题的解决