大佬教程收集整理的这篇文章主要介绍了Jetpack Compose和View的互操作性,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Compose风这么大, 对于已有项目使用新技术, 难免会担心兼容性. 对于Compose来说, 至少和View的结合是无缝的. (目前来讲, 已有项目要采用Compose, 可能初期要解决的就是升级gradle plugin, gradle, Android studio, kotlin之类的问题.)
构建UI的灵活性还是有保证的:
本文就是一些互相调用的简单小demo, 初期用的时候可以复制粘贴一下很趁手.
官方文档: https://developer.android wangt.cc /jetpack/compose/interop/interop-apis
class ExampleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceStatE)
setContent { // In here, we can call composables!
MaterialTheme {
GreeTing(name = "compose")
}
}
}
}
@Composable
fun GreeTing(name: String) {
Text(text = "Hello $name!")
}
class PureComposeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
MaterialTheme {
Text("Hello Compose!")
}
}
}
}
}
一个平平无奇的xml布局文件中加入ComposeView
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_627_11845@as.android wangt.cc /apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/Hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello from XML layout" />
<androidx wangt.cc pose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
使用的时候, 先根据id查找出来, 再setContent:
class ComposeViewInXmlActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceStatE)
setContentView(R.layout.activity_compose_view_in_xml)
findViewById<ComposeView>(R.id wangt.cc pose_view).setContent {
// In Compose world
MaterialTheme {
Text("Hello Compose!")
}
}
}
}
在代码中使用addView()
来添加View对于ComposeView
来说也同样适用:
class ComposeViewInViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceStatE)
setContentView(LinearLayout(this).apply {
orientation = VERTICAL
addView(ComposeView(this@ComposeViewInViewActivity).apply {
id = R.id wangt.cc pose_view_x
setContent {
MaterialTheme {
Text("Hello Compose View 1")
}
}
})
addView(TextView(context).apply {
text = "I'm am old TextView"
})
addView(ComposeView(context).apply {
id = R.id wangt.cc pose_view_y
setContent {
MaterialTheme {
Text("Hello Compose View 2")
}
}
})
})
}
}
这里在LinearLayout
中添加了三个child: 两个ComposeView
中间还有一个TextView
.
起到桥梁作用的ComposeView
是一个ViewGroup
, 它本身是一个View, 所以可以混进View的hierarchy tree里占位,
它的setContent()
方法开启了Compose世界的大门, 在这里可以传入composable的方法, 绘制UI.
都用Compose搭建UI了, 什么时候会需要在其中内嵌View呢?
AdView
, @H_933_27@mapView, WebView
.@H_675_8@
例子:
@Composable
fun CustomView() {
val state = remember { mutableStateOf(0) }
//widget.button
AndroidView(
factory = { ctx ->
//Here you can construct your View
android.widget.button(ctX).apply {
text = "My Button"
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
setOnClickListener {
state.value++
}
}
},
modifier = Modifier.padding(8.dp)
)
//widget.TextView
AndroidView(factory = { ctx ->
//Here you can construct your View
TextView(ctX).apply {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
}
}, @R_674_9531@e = {
it.text = "You have clicked the buttons: " + state.value.toString() + " times"
})
}
这里的桥梁是AndroidView
, 它是一个composable方法:
@Composable
fun <T : View> AndroidView(
factory: (Context) -> T,
modifier: Modifier = Modifier,
@R_674_9531@e: (T) -> Unit = NoOp@R_674_9531@e
)
factory接收一个Context参数, 用来构建一个View. @R_674_9531@e方法是一个callBACk, inflate之后会执行, 读取的状态state值变化后也会被执行.
上面提到的在Compose中使用AndroidView的方法, 对于少量的UI还行. 如果需要复用一个已经存在的xml布局怎么办? 不用怕, view binding登场了.
使用起来也很简单:
buildFeatures {
compose true
viewBinding true
}
complex_layout
.@H_675_8@
androidx wangt.cc pose.ui:ui-viewbinding
.@H_675_8@
然后build一下, 生成binding类, 这样就好了, 哒哒:
@Composable
private fun ComposableFromLayout() {
AndroidViewBinding(ComplexLayoutBinding::inflatE) {
sampleButton.setBACkgroundColor(Color.GRAY)
}
}
其中ComplexLayoutBinding
是根据布局名字生成的类.
AndroidViewBinding
内部还是调用了AndroidView
这个composable方法.
这个场景听上去有点奇葩, 因为Compose的设计理念, 貌似就是为了跟Fragment说再见. 在Compose构建的UI中, 再找地方显示一个Fragment, 有点新瓶装旧酒的意思.
但是遇到的场景多了, 你没准真能遇上呢.
Fragment通过FragmentManager添加, 需要一个布局容器. 把上面ViewBinding的例子改改, 布局里加入一个fragmentContainer, 点击显示Fragment:
column(Modifier.fillMaxSize()) {
Text("I'm a Compose Text!")
Button(
onClick = {
showFragment()
}
) {
Text(text = "Show Fragment")
}
ComposableFromLayout()
}
@Composable
private fun ComposableFromLayout() {
AndroidViewBinding(
FragmentConTrainerBinding::inflate,
modifier = Modifier.fillMaxSize()
) {
}
}
private fun showFragment() {
supportFragmentManager
.begintransaction()
.add(R.id.fragmentContainer, PureComposeFragment())
wangt.cc mit()
}
这里没有考虑时机的问题, 因为点击按钮展示Fragment, 将时机拖后了. 如果直接在初始化的时候想显示Fragment, 可能会抛出异常:
java.lang.IllegalArgumentexception: No view found for id
解决办法:
@Composable
private fun ComposableFromLayout() {
AndroidViewBinding(
FragmentConTrainerBinding::inflate,
modifier = Modifier.fillMaxSize()
) {
// here is safe
showFragment()
}
}
所以show的时机至少要保证container view已经inflated了.
迁移View的app到Compose, 你可能会需要Theme Adapter: https://github wangt.cc /material-components/material-components-android-compose-theme-adapter
关于在现有的view app中使用COR_627_11845@pose: https://developer.android wangt.cc /jetpack/compose/interop/compose-in-exisTing-ui
Compose和View的结合, 主要是靠两个桥梁. 还挺有趣的:
Compose和View可以互相兼容的特点保证了项目可以逐步迁移, 并且也给够了安全感, 像极了当年java项目迁移kotlin. 至于什么学习曲线, 经验不足, 反正早晚都要学的, 整点新鲜的也挺好, 亦可赛艇.
以上是大佬教程为你收集整理的Jetpack Compose和View的互操作性全部内容,希望文章能够帮你解决Jetpack Compose和View的互操作性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。