How to select item(s) out of a FlatList?

若如初见. 提交于 2019-12-13 12:15:49

问题


I want to select items using FlatList like when you select multiple photos on your photo galery (in this case, I am using a flatlist with 2 rows like a grid of 2x10). I want to select (for example) 3 items using onLongPress.

Something like this, but with 2 rows.


回答1:


What about this:

Every item rendered would have an onLongPress, and an isPicked property.

Initially, and in renderItem(), you would implement an if statement that either renders checked square or empty square every time an item executes executes; To indicate if the item is selected or not (based on isPicked).

onLongPress invokes isPicked and rerenders everything (to toggle the square). So if isPicked is true it becomes false, and if false becomes true. You can achieve that by simply doing this: this.item.isPicked = !this.item.isPicked

Finally on Submit, filter all your initial items (data) by the isPicked === true and hurrah, you will end up with the items that were selected !



来源:https://stackoverflow.com/questions/45358951/how-to-select-items-out-of-a-flatlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!