【问题标题】:Android Compose - Use traditional View with ComposeViewAndroid Compose - 将传统视图与 ComposeView 结合使用
【发布时间】:2021-05-25 04:47:47
【问题描述】:

我将 Compose 与现有片段一起使用。我在 xml 中的结构

<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <ImageView />
            <MaterialToolbar />
        </CollapsingToolbarLayout>
    </AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.compose.ui.platform.ComposeView
            android:id="@+id/composeContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.core.widget.NestedScrollView>
</CoordinatorLayout>

在 ComposeView 中,我使用 LazyColum,它与日志一起崩溃

java.lang.IllegalStateException: Nesting scrollable in the same direction layouts like ScrollableContainer and LazyColumn is not allowed. If you want to add a header before the list of items please take a look on LazyColumn component which has a DSL api which allows to first add a header via item() function and then the list of items via items().

我认为该错误的原因是 LazyColum(可滚动)放在其他可滚动(NestedScrollView)中。但是如果我删除 NestedScrollView,它看起来像

<androidx.compose.ui.platform.ComposeView
    android:id="@+id/composeContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

当我在 LazyColumn 中滚动列表时,CollapsingToolbarLayout 没有滚动。那是下一个错误。

如何解决?

请帮助我。谢谢。

【问题讨论】:

标签: android android-collapsingtoolbarlayout android-jetpack-compose


【解决方案1】:

我认为这是一个问题,所以我打开了一个。

https://issuetracker.google.com/issues/199693522

事实证明这不是问题。

根据撰写团队的回复,LazyColumn 看起来有一个非无限的高度。通过不给你的 LazyColumn 一个固定的高度并让你的 ComposeView 的高度 match_parent (这是无限的,因为滚动视图的高度是无限的),LazyColumn 不能工作,因为它现在有无限的高度。

【讨论】:

    【解决方案2】:

    &lt;androidx.core.widget.NestedScrollView&gt; 解开您的撰写视图,并使用来自 Chris Banes 的this 解决方法。它就像一个魅力。只需按照 cmets 中的说明进行操作即可:

     * setContent {
     *     Surface(
     *         // Add this somewhere near the top of your layout, above any scrolling layouts
     *         modifier = Modifier.nestedScroll(rememberViewInteropNestedScrollConnection())
     *     ) {
     *         LazyColumn() {
     *             // blah
     *         }
     *     }
     * }
    

    这是一个现有的 Compose 错误,可以在 issue tracker 中找到。

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2011-10-07
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多