我的 RecyclerView.ViewHolder 可以是非静态外部 class 吗?
Can my RecyclerView.ViewHolder s be a non-static outer class?
我的 RecyclerView
中有多个 View
类型。我知道我需要写多个 RecyclerView.ViewHolder
s.
我正在尝试将它们写在不同的包中,以使整个项目组织更清晰。
我知道在对 ListView 使用 ViewHolder
模式时,ViewHolder
s 应该(must/can?)在 类.
RecyclerView
和 ListView
的 ViewHolder
是否可以是非静态和非内部的,并且仍然保留它们预期的性能优势?
是。最佳做法是为 RecyclerView.ViewHolder
使用单独的 non-static class 创建 recyclerview 适配器。好处是:
By creating inner static class and separate , It is reusable in the case of same RecyclerView.ViewHolder
for other adapters.
If your using RecyclerView.ViewHolder
in only single adapter, you can declare it into inner class of your adapter.
谈谈性能 :
根据GC performance hit for inner class vs. static nested class和recyclerview的优势:
While using RecyclerView
, it's recycle the instances viewholder, so the memory impact is not a problem. The static one will take less memory than the other one.
查看 and 好的答案以获得更多想法。
希望对你有所帮助。
我的 RecyclerView
中有多个 View
类型。我知道我需要写多个 RecyclerView.ViewHolder
s.
我正在尝试将它们写在不同的包中,以使整个项目组织更清晰。
我知道在对 ListView 使用 ViewHolder
模式时,ViewHolder
s 应该(must/can?)在 类.
RecyclerView
和 ListView
的 ViewHolder
是否可以是非静态和非内部的,并且仍然保留它们预期的性能优势?
是。最佳做法是为 RecyclerView.ViewHolder
使用单独的 non-static class 创建 recyclerview 适配器。好处是:
By creating inner static class and separate , It is reusable in the case of same
RecyclerView.ViewHolder
for other adapters.If your using
RecyclerView.ViewHolder
in only single adapter, you can declare it into inner class of your adapter.
谈谈性能 :
根据GC performance hit for inner class vs. static nested class和recyclerview的优势:
While using
RecyclerView
, it's recycle the instances viewholder, so the memory impact is not a problem. The static one will take less memory than the other one.
查看
希望对你有所帮助。