用于 RecyclerView 的自定义 LayoutManager 和 ItemDecoration

重要

本文介绍的功能和指南为公共预览版,在正式发布之前可能会有重大修改。 Microsoft 不对此处提供的信息作任何明示或默示的担保。

FoldableLayoutManagerLinearLayoutManagerGridLayoutManager 的包装器,将根据应用是否跨显示屏来提供其中的一个或另一个。

FoldableItemDecorationRecyclerView. ItemDecoration 的实现,它将在这两列之间创建一个边距,这样一来,当应用跨屏幕并使用 FoldableLayoutManager 时,铰链(如果存在)就不会覆盖列。

在单屏幕上,使用 FoldableLayoutManagerFoldableItemDecoration 的 RecyclerView 看起来与通常时一样:

Surface Duo Emulator displaying an application on the left screen with items of the same size

在跨屏幕模式下,使用 FoldableLayoutManagerFoldableItemDecoration 的 RecyclerView 将在两个屏幕之间拆分内容:

Surface Duo Emulator displaying a spanned application with items of the same size

1.0.0-beta4 版本开始,使用 FoldableLayoutManagerFoldableItemDecoration 的 RecyclerView 也会在可折叠设备上的 FoldingFeature 中拆分内容。 例如,这是它在 6.7 英寸水平折叠仿真器上的显示效果:

Foldable Emulator displaying an application on the whole screen with items of the same size

class MainActivity : AppCompatActivity() {
//...
    
    private fun onWindowLayoutInfoChanged(windowLayoutInfo: WindowLayoutInfo) {
        recyclerView.layoutManager = FoldableLayoutManager(this, windowLayoutInfo).get()
        recyclerView.replaceItemDecorationAt(FoldableItemDecoration(windowLayoutInfo))
    }
}

FoldableStaggeredLayoutManager 和 FoldableStaggeredItemDecoration

还有一种方法可以使 StaggeredGridLayoutManager 处于双屏模式,方法是包含需要与 FoldableStaggeredItemDecoration 一起使用的 FoldableStaggeredLayoutManager

在单屏幕上,使用 FoldableStaggeredLayoutManagerFoldableStaggeredItemDecoration 的 RecyclerView 看起来与通常时一样:

Surface Duo Emulator displaying an application on the left screen with items of variable sizes

在跨屏幕模式下,使用 FoldableStaggeredLayoutManagerFoldableStaggeredItemDecoration 的 RecyclerView 将在两个屏幕之间拆分内容:

Surface Duo Emulator displaying a spanned application with items of variable sizes

1.0.0-beta4 版本开始,使用 FoldableStaggeredLayoutManagerFoldableStaggeredItemDecoration 的 RecyclerView 也会在可折叠设备上的两个屏幕之间拆分内容。 例如,这是它在 6.7 英寸水平折叠仿真器上的显示效果:

Foldable Emulator displaying an application on the whole screen with items of variable sizes

class MainActivity : AppCompatActivity() {
//...
    
    private fun onWindowLayoutInfoChanged(windowLayoutInfo: WindowLayoutInfo) {
        recyclerView.layoutManager = FoldableStaggeredLayoutManager(this, windowLayoutInfo).get()
        recyclerView.replaceItemDecorationAt(FoldableStaggeredItemDecoration(windowLayoutInfo))
    }
}