Compose和AndroidView的交互

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方法中調用。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容