英文:
Filter in RecyclerView
问题
我有一个已初始化数据的RecyclerView,类似这样:
数据类 ObjectData(
var name: String,
var pass: Boolean
) : Serializable
我正在使用 ArrayList
问题:对此最佳的方法是什么?我可以保留3个列表视图,一个包含所有数据,一个包含已通过的对象,一个包含未通过的对象,并在更改筛选器时使用所需列表更新适配器。但这会导致数据重复。我可以在筛选器上将所有数据复制到新的筛选列表,但这会在每次按钮筛选点击时触发复制,并且用户可能会快速更改筛选器。
你有什么建议。
英文:
I have RecyclerView witch initialized with data like this:
data class ObjectData(
var name: String,
var pass: Boolean
) : Serializable
I am Initialing the Adapter with ArrayList<ObjectData>. In my Activity I have filter (All, Passed, No Passed). When I click one of the filters I wish that RecyclerView refresh with new data regarding the "pass" value. Example: On "Passed" filter button it will show only ObjectData with pass=true.
Question: What is the best approach to this? I can hold 3 list views, one with all data, one with passed objects and one with unpassed, and update adapter with the needed list when filter is changed. But this will cause data duplication. I can on filter copy all data to new filtered list, but it will trigger copy on every button filter click, and the user may change filters rapidly.
What can you suggest.
答案1
得分: 1
我只会翻译代码部分,不会回答关于翻译的问题。
我只会翻译代码部分,不会回答关于翻译的问题。
英文:
I would simply create copies lazily and cache them for example like this:
data class ObjectData(
var name: String,
var pass: Boolean
) : Serializable
val allData: List<ObjectData> = emptyList()
val filteredData: Map<Boolean, List<ObjectData>> by lazy {
allData.groupBy { it.pass }
}
This way you'll create both filtered lists in one go and only when you need them.
Honestly if data set is not big i wouldn't really optimise prematurely unless i see performance is bad. Nowadays Android has much better memory handling.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论