【发布时间】:2021-10-01 09:10:09
【问题描述】:
我是 Android 新手
我在 RecyclerView 中显示列表项时遇到问题。 我希望一个一个显示列表并在它仍在加载时追加。
但是发生的情况是,在获取所有项目后会出现列表。
这是我的片段
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_lock_list, container, false)
val recyclerView = view.findViewById<RecyclerView>(R.id.list)
for(device in devices) {
fetchBuildingAddress(device)
}
if (recyclerView is RecyclerView) {
with(recyclerView) {
layoutManager = LinearLayoutManager(context)
adapter = deviceRecyclerViewAdapter
}
}
return view
}
private fun fetchBuildingAddress(device: Device) {
deviceWearableService.getBuilding(device.buildingId) {
when(it) {
is DeviceWearableServiceImpl.State.Success -> {
val buildingName = it.resources.buildingAddress.address1
val deviceBuilding = DeviceBuillding(device, buildingName)
deviceRecyclerViewAdapter.insertDevice(deviceBuilding)
}
else -> {
// TODO: ERROR STATE
}
}
}
}
在我的insertDevice
我只是在适配器中调用notifyItemInserted。
fun insertDevice(updateDeviceBuilding: DeviceBuillding) {
deviceBuilding.add(updateDeviceBuilding)
notifyItemInserted(deviceBuilding.size - 1 )
}
您还可以提出更好的行为建议!非常感谢您。
【问题讨论】:
-
这可能是因为
deviceWearableService.getBuilding因为您在循环中一个接一个地调用它而没有任何延迟,因此所有项目可能会在几毫秒的差异内一次加载。加载一次需要多长时间?deviceWearableService.getBuilding到底是什么?执行时间是固定的还是随机的? -
您好!你可能是对的,因为它只获取建筑细节。它是一个 API。我想知道应该如何处理。