admin管理员组文章数量:1302502
On the documentation they say this:
Basic usage Implement ListDetailPaneScaffold as follows:
Use a class that represents the content to be selected. This class should be Parcelable to support saving and restoring the selected list item. Use the kotlin-parcelize plugin to generate the code for you.
@Parcelize
class MyItem(val id: Int) : Parcelable
And later, they use that this way:
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
Why they ask you to use a custom class? Why not simply using an Int representing the current index clicked? Simply this:
val navigator = rememberListDetailPaneScaffoldNavigator<Int>()
Am I losing something? I tested and it works. It even survives configuration changes without having to deal with Parcelable. It simplifies everything. Why adding the complexity of a custom Parcelable Class instead of simply using a Int index?
On the documentation they say this:
Basic usage Implement ListDetailPaneScaffold as follows:
Use a class that represents the content to be selected. This class should be Parcelable to support saving and restoring the selected list item. Use the kotlin-parcelize plugin to generate the code for you.
@Parcelize
class MyItem(val id: Int) : Parcelable
And later, they use that this way:
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
Why they ask you to use a custom class? Why not simply using an Int representing the current index clicked? Simply this:
val navigator = rememberListDetailPaneScaffoldNavigator<Int>()
Am I losing something? I tested and it works. It even survives configuration changes without having to deal with Parcelable. It simplifies everything. Why adding the complexity of a custom Parcelable Class instead of simply using a Int index?
Share edited Feb 10 at 13:16 NullPointerException asked Feb 10 at 12:58 NullPointerExceptionNullPointerException 37.7k80 gold badges230 silver badges403 bronze badges1 Answer
Reset to default 0That is just an example. Of course you could use rememberListDetailPaneScaffoldNavigator<Int>()
instead when the data class only contains an Int. But it shows how you can handle more complex objects as well.
In a classic, non-adaptive layout you would pass a MyItem
object from your list composable to the details composable. MyItem
would be a part of the ui state, containing the details of the currently selected list item. Usually that will be much more than just a simple Int. Imagine a list of persons where the user can select one person to display the person's details. Then you would need a custom object with many more properties than just an id, like a first name, last name, date of birth, country of origin, and so on.
By making that object parcelable it can also be used in the ListDetailPaneScaffold where it needs to be placed on the navigation's backstack (in a serialized form) and saved in the file system as a Bundle to survive configuration changes and system-initiated process death. That are the same requirements for saving an object with rememberSaveable
.
If you do not need to pass anything to the details composable you can use the rememberListDetailPaneScaffoldNavigator
overload without a type parameter.
本文标签: androidWhy ListDetailPaneScaffold needs a custom Parcelable classStack Overflow
版权声明:本文标题:android - Why ListDetailPaneScaffold needs a custom Parcelable class? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741715786a2394101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论