【接口方式】Android Fragment向Activity传递数据

参考【看后面接口方式的讲解】:https://www.bilibili.com/video/av467917327/?vd_source=36b998b8d52bd4dca85291afa2ad1a44

1、fragment_first.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">

    <Button
        android:id="@+id/fragment_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="fragment向Activity传参【接口方式】"
        android:backgroundTint="#f00"/>

</LinearLayout>

2、FirstFragment.kt

package com.example.kotlingradletemplatefirst.myfragment


import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.fragment.app.Fragment
import com.example.kotlingradletemplatefirst.R
import kotlin.random.Random


class FirstFragment : Fragment(R.layout.fragment_first) {
    /**
     * 1、创建一个属性来保存Activity传入的间谍对象!
     */
    lateinit var ownFragmentDataChangeListener: FragmentDataChangeListener;

    fun interface FragmentDataChangeListener {
        /**
         * 1、定义一个SMA函数式接口
         */
        fun onDataChangeListener(data: String)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.findViewById<Button>(R.id.fragment_button).setOnClickListener {
            /**
             * 1、基于【Fragment向Activity传参】的接口方式!
             */
            this.ownFragmentDataChangeListener.onDataChangeListener(
                "我是fragment的参数TextView内容【接口方式】${Random.nextInt()}"
            )
        }
    }

}

3、activity_first.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">

    <TextView
        android:id="@+id/activity_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:text="接收fragment的参数" />

    <!--
        1、这里基于FragmentContainerView框架嵌入fragment的xml代码
        2、这里指定name属性,则代表静态加载,未指定则动态加载
    -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_first"
        android:name="com.example.kotlingradletemplatefirst.myfragment.FirstFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

</LinearLayout>

4、FirstActivity.kt

package com.example.kotlingradletemplatefirst

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlingradletemplatefirst.myfragment.FirstFragment

class FirstActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        /**
         * 1、第一次创建Activity对象时,即给【匹配的fragment对象】派一个间谍绑定属性【ownFragmentDataChangeListener】
         * 2、只要【匹配的fragment对象】一直存在,则ownFragmentDataChangeListener属性则一直存在!
         * 3、务必注意:这里必须使用supportFragmentManager来或许已存在的fragment对象,不能新创建!!!
         */
        val firstFragment = (
                supportFragmentManager.findFragmentById(R.id.fragment_container_first) as FirstFragment)
        firstFragment.ownFragmentDataChangeListener = FirstFragment.FragmentDataChangeListener {
            findViewById<TextView>(R.id.activity_view).text = it
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值