網站首頁 編程語言 正文
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交互,將會可能涉及到LiveData
和State
數據的交換問題。
由于在AndroidView中常用LiveData來進行數據的訂閱,而在Compose中使用的是Compose特有的mutableStateOf
或者mutableListStateOf
等來進行ReCompose
,UI更新,所以當同時存在兩者的時候,需要將
LiveData轉換成Compose中的State對象,然后才能在Compose中實現訂閱功能。
Compose庫中提供了一個擴展函數來進行LiveData
和State
之間進行轉換:
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方法中調用。
原文鏈接:https://blog.csdn.net/cj641809386/article/details/126642139
相關推薦
- 2022-08-20 oracle設置密碼復雜度及設置超時退出的功能_oracle
- 2022-06-01 分享3個簡單的Python代碼高效運行技巧_python
- 2022-09-18 Python?Pandas實現DataFrame合并的圖文教程_python
- 2021-12-03 C++類和對象實戰之Date類的實現方法_C 語言
- 2022-09-21 Flutter實現資源下載斷點續傳的示例代碼_Android
- 2022-05-10 原生ajax 在服務器響應前撤銷請求
- 2022-11-14 Go語言文件讀寫操作案例詳解_Golang
- 2023-10-25 用原生promise特性替代async/await解決異步的方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支