0

I want to update one item calling asynchronous call to server API inside onBindViewHolder. I send item at position + position into the callback. Response call arrives in 20 seconds into callback funcion. Into the callback function is returned response and position. Then I call notifyItemChanged(pos, response). And inside payload onBindViewHolder I update item on that position and its View. Problem is that if async request takes 20 seconds and user will scroll quickly throught RecyclerView it will update wrong item View because that view at position could be unbound or could be bound to other item from list.

Is there any way how to track which item at which position is unbound to cancel request? I have to update this one info after adapter is created, because if I will do that before, user will not see any recyclerView item for almost 2 minutes till my list is ready. (only this one call is taking so long).

martin1337
  • 2,384
  • 6
  • 38
  • 85

2 Answers2

0

I wouldn't pass the position as context. I would pass an identifier. Could be an internal integer as id or something. Then you can easily determine which id's are currently visible on the screen and see if that list contains the id of the calback.

Loren Rogers
  • 325
  • 3
  • 13
0

I am not sure about your question, but you could work onto your items directly in your adapter. Then you do:

Adapter adapter = new BaseAdapter(newDataset);
recyclerView.setAdapter(adapter);
recyclerView.notifyDataSetChanged();

Otherwise, if your really work on views rather than dataset, you can do this: in your adapter:

  • create a View array
  • create a custom function View getViewAtPos(int pos)
  • check if your view is visible in your current scrollview (here)
Besoul
  • 35
  • 6