LayoutManager et ItemDecoration personnalisés pour RecyclerView

Important

Les fonctionnalités et l’aide décrites dans cet article sont en préversion publique et peuvent faire l’objet de modifications importantes avant leur lancement en disponibilité générale. Microsoft ne donne aucune garantie, expresse ou implicite, concernant les informations fournies ici.

Le FoldableLayoutManager est un wrapper sur un LinearLayoutManager et un GridLayoutManager qui va fournie l’un ou l’autre selon que l’application est répartie ou non sur des écrans.

Le FoldableItemDecoration est une implémentation d’un RecyclerView.ItemDecoration qui crée une marge entre les deux colonnes afin que celles-ci ne soient pas cachées par la charnière (le cas échéant) quand l’application est répartie sur deux écrans et que le FoldableLayoutManager est utilisé.

Sur un seul écran, une RecyclerView qui utilise FoldableLayoutManager et FoldableItemDecoration se présente comme d’habitude :

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

En mode réparti, une RecyclerView qui utilise FoldableLayoutManager et FoldableItemDecoration divise le contenu entre les deux écrans :

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

À compter de la version 1.0.0-beta4, une RecyclerView qui utilise FoldableLayoutManager et FoldableItemDecoration va également diviser le contenu avec FoldingFeature sur les appareils pliables. Par exemple, voici comment il se présenterait sur l’Émulateur de pliage horizontal de 6,7 pouces :

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 et FoldableStaggeredItemDecoration

Il y a aussi un moyen d’avoir StaggeredGridLayoutManager en mode double écran, en incluant FoldableStaggeredLayoutManager qui doit être utilisé avec FoldableStaggeredItemDecoration.

Sur un seul écran, une RecyclerView qui utilise FoldableStaggeredLayoutManager et FoldableStaggeredItemDecoration se présente comme d’habitude :

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

En mode réparti, une RecyclerView qui utilise FoldableStaggeredLayoutManager et FoldableStaggeredItemDecoration divise le contenu entre les deux écrans :

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

À compter de la version 1.0.0-beta4, une RecyclerView qui utilise FoldableStaggeredLayoutManager et FoldableStaggeredItemDecoration va également diviser le contenu entre les deux écrans sur les appareils pliables. Par exemple, voici comment il se présenterait sur l’Émulateur de pliage horizontal de 6,7 pouces :

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))
    }
}