假设有一个简单的场景:
user
用于存储用户详细信息的表user_images
以将图像的路径存储在手机的存储中的image_path
字段中。RecyclerView
中显示了一个用户列表,用户可以滑动以删除行。适配器为ListAdapter
,数据来自房间,为LiveData
我在想的工作流程是这样的:
ListAdapter
中重新添加项目并更新recyclerview。如果用户未按“撤消”,并且超时后小吃被取消,则从user
,user_images
中删除行,并从user_images
]中与image_path
相关的存储中删除所有图像。...
override fun onSwiped(...){ val deletedItem = listAdapter.currentList.get(index) //REMOVE FROM THE ADAPTER HERE val snack = Snackbar.make(...) snack.setAction(...){ //RE-ADD TO ADAPTER HERE } snack.addCallback(...){ override fun onDismissed(...){ //do the actual deletes } } }
...
问题是使用ListAdapter。如您所知,它使用DiffCallBack来管理视图更新,并且调用adapter.submitList(list)
不会触发任何更新,因为它接收到相同的列表。 (ListAdapter not updating item in reyclerview)因此,为了能够删除项目,我需要:
currentList
作为Mutable
updatebleList获取>>adapter.submitList(updatebleList.toList())
adapter.submitList(updatebleList.toList())
如您所见,为了正确提交,创建了大量列表。有没有更好/更简单的方法来实现这一目标?
假设有一个简单的场景:用于存储用户详细信息的用户表user_images将image_path字段存储在电话存储中的图像路径。有显示的用户列表...
我只是通过创建新的ItemTouchHelper()只需滑动即可删除MainActivity中的功能。您可以在GitHub here上查看示例代码。