我的 RecyclerView
中有多个 View
类型。我知道我需要编写多个 RecyclerView.ViewHolder
。
我正在尝试将它们编写在不同的包中,以使整个项目组织更加清晰。
我知道,在对 ListView 使用 ViewHolder
模式时,ViewHolder
应该(必须/可以?)设为静态内部类。
RecyclerView
和ListView
的ViewHolder
是否可以是非静态和非内部的,并且仍然保留其预期的性能优势?
最佳答案
是。最佳实践是使用 RecyclerView.ViewHolder
的单独非静态类创建 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.
希望对你有帮助。
关于android - 我的 RecyclerView.ViewHolder 可以是非静态外部类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39178549/