Comments (8)
头疼啊,公司内部的 androidx 版本普通很旧,所以这个框架也不好直接升级 androidx 的依赖
from scene.
from scene.
疑,你是怎么使用的,Compose 这个东西不是理论上可以内嵌到任何的 ViewGroup 里面吗?
from scene.
https://developer.android.google.cn/jetpack/compose/interop/adding#reuse-view
假设我们要在应用中将用户问候文本迁移到 Jetpack Compose。我们可以在 XML 布局中添加以下内容:
为了将其迁移到 Compose,我们可以将 View 替换为保留了相同布局参数和 id 的 ComposeView:
然后,在使用了该 XML 布局的 Activity 或 Fragment 中,我们可以获取 ComposeView,并调用 setContent 方法,以向其中添加 Compose 内容:
我是按照这种方式来做的
class ComposeScene : Scene() {
private lateinit var composeView : ComposeView
override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
composeView = ComposeView(requireSceneContext())
return composeView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
composeView.setContent {
CardTheme {
ProvideWindowInsets {
val systemUiController = rememberSystemUiController()
val darkIcons = MaterialTheme.colors.isLight
SideEffect {
systemUiController.setSystemBarsColor(Color.Transparent, darkIcons = darkIcons)
}
val navController = rememberNavController()
// val navigationActions = remember(navController){
//
// }
Greeting()
}
}
}
}
@Composable
private fun Greeting() {
Text(
text = "我是测试文字",
modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp)
)
}
}
from scene.
在你的首页的 Activity 的 onCreate 里面补充这段代码
ViewTreeLifecycleOwner.set(window.decorView, this)
ViewTreeViewModelStoreOwner.set(window.decorView, this)
ViewTreeSavedStateRegistryOwner.set(window.decorView, this)
from scene.
还有一种做法,我感觉这种更合适
import android.view.View
import androidx.compose.runtime.MonotonicFrameClock
import androidx.compose.runtime.PausableMonotonicFrameClock
import androidx.compose.runtime.Recomposer
import androidx.compose.ui.platform.AndroidUiDispatcher
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.bytedance.scene.Scene
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.launch
import kotlin.coroutines.EmptyCoroutineContext
fun Scene.createLifecycleAwareViewTreeRecomposer(): Recomposer {
val currentThreadContext = AndroidUiDispatcher.CurrentThread
val pausableClock = currentThreadContext[MonotonicFrameClock]?.let {
PausableMonotonicFrameClock(it).apply { pause() }
}
val contextWithClock = currentThreadContext + (pausableClock ?: EmptyCoroutineContext)
val recomposer = Recomposer(contextWithClock)
val runRecomposeScope = CoroutineScope(contextWithClock)
val viewTreeLifecycleOwner = this
// Removing the view holding the ViewTreeRecomposer means we may never be reattached again.
// Since this factory function is used to create a new recomposer for each invocation and
// doesn't reuse a single instance like other factories might, shut it down whenever it
// becomes detached. This can easily happen as part of setting a new content view.
this.view.addOnAttachStateChangeListener(
object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View?) {}
override fun onViewDetachedFromWindow(v: View?) {
this@createLifecycleAwareViewTreeRecomposer.view.removeOnAttachStateChangeListener(this)
recomposer.cancel()
}
}
)
viewTreeLifecycleOwner.lifecycle.addObserver(
object : LifecycleEventObserver {
override fun onStateChanged(lifecycleOwner: LifecycleOwner, event: Lifecycle.Event) {
val self = this
when (event) {
Lifecycle.Event.ON_CREATE ->
// Undispatched launch since we've configured this scope
// to be on the UI thread
runRecomposeScope.launch(start = CoroutineStart.UNDISPATCHED) {
try {
recomposer.runRecomposeAndApplyChanges()
} finally {
// If runRecomposeAndApplyChanges returns or this coroutine is
// cancelled it means we no longer care about this lifecycle.
// Clean up the dangling references tied to this observer.
lifecycleOwner.lifecycle.removeObserver(self)
}
}
Lifecycle.Event.ON_START -> pausableClock?.resume()
Lifecycle.Event.ON_STOP -> pausableClock?.pause()
Lifecycle.Event.ON_DESTROY -> {
recomposer.cancel()
}
Lifecycle.Event.ON_PAUSE -> {
// Nothing
}
Lifecycle.Event.ON_RESUME -> {
// Nothing
}
Lifecycle.Event.ON_ANY -> {
// Nothing
}
}
}
}
)
return recomposer
}
然后你的 Scene,补充 composeView.setParentCompositionContext(createLifecycleAwareViewTreeRecomposer())
class ComposeScene : Scene() {
private lateinit var composeView : ComposeView
override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
composeView = ComposeView(requireSceneContext())
return composeView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewTreeLifecycleOwner.set(this.view, this)
ViewTreeViewModelStoreOwner.set(this.view, this)
ViewTreeSavedStateRegistryOwner.set(this.view, fragmentActivity())
composeView.setParentCompositionContext(createLifecycleAwareViewTreeRecomposer())
composeView.setContent {
CardTheme {
ProvideWindowInsets {
val systemUiController = rememberSystemUiController()
val darkIcons = MaterialTheme.colors.isLight
SideEffect {
systemUiController.setSystemBarsColor(Color.Transparent, darkIcons = darkIcons)
}
val navController = rememberNavController()
// val navigationActions = remember(navController){
//
// }
Greeting()
}
}
}
}
@Composable
private fun Greeting() {
Text(
text = "我是测试文字",
modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp)
)
}
}
from scene.
好的...我试一哈,多谢大佬指点ღ( ´・ᴗ・` )
from scene.
@qii 是可以的,多谢!!
from scene.
Related Issues (20)
- SceneContainerActivity透明主题 HOT 4
- need release resources on OnDestroyView? HOT 4
- Viewpager2 adapter HOT 2
- Why not using material components? HOT 1
- Deep linking? HOT 1
- Demo singleTask 用法和singleTop 一样啊,不会清除要启动的sense 栈上面的sense HOT 1
- scene跨进程打开另外一个页面 可以解决白屏问题吗? HOT 5
- 出现空指针 HOT 2
- 出现崩溃 HOT 2
- 现状和计划 HOT 8
- 如何在Scene里面监听按键 HOT 1
- 无法拉取新版本更新依赖的问题。 HOT 8
- 关于权限请求的问题 HOT 1
- 觉得这个项目和Jetpack Compose 很适合一起使用 HOT 1
- 结合ViewBinding使用的回收时机 HOT 1
- 绑定到Fragment时onViewCreated生命周期 HOT 5
- JCenter deprecated HOT 1
- 项目编译不过 HOT 2
- BottomNavigationViewScene的底部menu点击事件回调能否传出来? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scene.