ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Compose和AndroidView的交互

2022-08-20 09:33:19  阅读:149  来源: 互联网

标签:Compose name AndroidView LiveData State 交互 加载


1、在 ComposeUI 中加载 AndroidView 控件

Compose 中可以加载 AndroidView 还是比较简单的,直接引入 AndroidView 来加载 AndroidView 布局文件。

@Composable
fun Greeting(name: String) {
    Column {
        Text(text = "Hello $name!")
        LoadAndroidView(name)
    }
}

/**
 * Compose中加载AndroidView
 */
@Composable
fun LoadAndroidView(name: String) {
    var tvTittle: TextView? = null
    AndroidView(factory = {
        //加载AndroidView布局。
        LayoutInflater.from(it).inflate(R.layout.activity_main, null).apply {
            tvTittle = findViewById(R.id.tvTittle)
        }
    }) {
        //更新UI数据
        tvTittle?.text = name
    }
}

factory参数主要是用来初始化 AndroidView 布局,将 AndroidView 布局通过工厂模式转换成 ComposeUI 加载到 Compose 中,只会执行一行,第二个回调函数,主要是用来更新 UI 数据,ReCompose可能会执行,所以我么初始化 AndroidView 的代码应该放在factory参数中。

2、在 AndroidView 中加载 ComposeUI

AndroidView 中引入 ComposeView 直接在 AndroidView 的布局文件中加入 androidx.compose.ui.platform.ComposeView

控件,在代码中初始化 ComposeView,调用setContent方法,就可以使用 ComposeUI 了。

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvTittle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是AndroidView" />

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/composeView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
class LoadComposeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<ComposeView>(R.id.composeView).apply { 
            setContent { 
                Column {
                    Text(text = "我是ComposeView")
                }
            }
        }
    }
}

3、LiveData 数据转换成 State 数据

LiveData 是 JetPack 组件的一部分,主要是在 AndroidView 中用来监听数据的变化,并且具有生命感知的,只有在 Activity 等处于活动才会触发数据更新。

State是 Compose 中特有的用来更新 Ui 的数据框架。比如常用的mutableStateOf , mutableListStateOf等。

当 AndroidView 和 Compose 交互,将会可能涉及到LiveDataState数据的交换问题。

由于在 AndroidView 中常用 LiveData 来进行数据的订阅,而在 Compose 中使用的是 Compose 特有的mutableStateOf或者mutableListStateOf等来进行ReCompose ,UI 更新,所以当同时存在两者的时候,需要将

LiveData 转换成 Compose 中的 State 对象,然后才能在 Compose 中实现订阅功能。

Compose 库中提供了一个扩展函数来进行LiveDataState之间进行转换:

1、导入 runtime-livedata 库

implementation 'androidx.compose.runtime:runtime-livedata:1.2.0'

2、将 LiveData 数据转换成 State 数据

private val tittleLv = MutableLiveData("Android")

private fun initView() {
    findViewById<ComposeView>(R.id.composeView).setContent {
        val tittle by tittleLv.observeAsState()
        Column {
            Text(text = tittle.orEmpty(),Modifier.clickable {
                tittleLv.value="测试LiveData转换State"
            })
        }
    }
}

调用 observeAsState 扩展函数可以将LiveData对象直接转换成State对象,在 Compose 中使用。

上面代码给Text加了个点击事件改变LiveData数据,Compose 中的文本同步改变是成功的。

需要注意的是,observeAsState 函数只能在 Compose 方法中调用。

标签:Compose,name,AndroidView,LiveData,State,交互,加载
来源: https://www.cnblogs.com/mahongyin/p/16607149.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有