欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AndroidView與Compose框架交互實現(xiàn)介紹

 更新時間:2022年09月06日 15:00:17   作者:兒歌八萬首  
Android Compose自推出正式版本后,google 就一直推薦使用Compose來開發(fā)。正好疫情期間,作為一個 Android 摸魚達人,就來摸索一下Compose的開發(fā)。說實話開發(fā)了2天感覺對Android 開發(fā)人員來說變化是巨大的,但是作為從業(yè)者我們還必須學習和學會,才能不被甩開

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數(shù)據(jù)
        tvTittle?.text = name
    }
}

factory參數(shù)主要是用來初始化AndroidView布局,將AndroidView布局通過工廠模式轉(zhuǎn)換成ComposeUI加載到Compose中,只會執(zhí)行一行,第二個回調(diào)函數(shù),主要是用來更新UI數(shù)據(jù),ReCompose可能會執(zhí)行,所以我么初始化AndroidView的代碼應(yīng)該放在factory參數(shù)中。

2、在AndroidView中加載ComposeUI

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

控件,在代碼中初始化ComposeView,調(diào)用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數(shù)據(jù)轉(zhuǎn)換成State數(shù)據(jù)

LiveData是JetPack組件的一部分,主要是在AndroidView中用來監(jiān)聽數(shù)據(jù)的變化,并且具有生命感知的,只有在Activity等處于活動才會觸發(fā)數(shù)據(jù)更新。

State是Compose中特有的用來更新Ui的數(shù)據(jù)框架。比如常用的mutableStateOf , mutableListStateOf等。

當AndroidView和Compose交互,將會可能涉及到LiveDataState數(shù)據(jù)的交換問題。

由于在AndroidView中常用LiveData來進行數(shù)據(jù)的訂閱,而在Compose中使用的是Compose特有的mutableStateOf或者mutableListStateOf等來進行ReCompose ,UI更新,所以當同時存在兩者的時候,需要將

LiveData轉(zhuǎn)換成Compose中的State對象,然后才能在Compose中實現(xiàn)訂閱功能。

Compose庫中提供了一個擴展函數(shù)來進行LiveDataState之間進行轉(zhuǎn)換:

1、導入runtime-livedata庫

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

2、將LiveData數(shù)據(jù)轉(zhuǎn)換成State數(shù)據(jù)

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轉(zhuǎn)換State"
            })
        }
    }
}

調(diào)用observeAsState擴展函數(shù)可以將LiveData對象直接轉(zhuǎn)換成State對象,在Compose中使用。

上面代碼給Text加了個點擊事件改變LiveData數(shù)據(jù),Compose中的文本同步改變是成功的。

需要注意的是,observeAsState函數(shù)只能在Compose方法中調(diào)用。

到此這篇關(guān)于AndroidView與Compose交互實現(xiàn)介紹的文章就介紹到這了,更多相關(guān)AndroidView Compose內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論