如何使用滚动片段容器实现通用布局?

问题描述 投票:1回答:1

我有一个活动的通用布局,只有一个片段:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的大多数活动都重复使用此布局,只是插入不同的片段。片段由ConstraintLayouts,LinearLayouts,RecyclerViews等组成。如果片段具有RecyclerView,则滚动效果很好。但是,如果它包含LinearLayout或ConstraintLayout,则无法滚动。如果将FrameLayout包装在NestedScrollView中,则可以滚动包含LinearLayout或ConstraintLayout的片段,但是它会破坏具有RecyclerView的片段中的回收:

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.core.widget.NestedScrollView>

如何使我的通用布局通用,并使其可以处理可能包含的任何类型的片段的滚动?

android android-fragments android-recyclerview android-coordinatorlayout android-nestedscrollview
1个回答
0
投票

有NestedScrollView Child的属性,

setNestedScrollingEnabled(false)

但是我强烈建议不要同时使用嵌套的滚动视图和回收视图,因为这可能会导致级联问题!我建议在RecyclerView中使用ItemViewTypes处理多种视图。只需添加一个具有match_parent宽度和高度的RecyclerView。然后在您的回收站视图适配器中覆盖getItemViewType并使用该位置来处理要放大的布局。

© www.soinside.com 2019 - 2024. All rights reserved.