问题
I think I've read pretty much all the answers to all of the similar SO questions, and NONE of them seem to be working for me. It's driving me nuts, and I've spent too much time on this.
I'm implementing a RecyclerView
here and I can't for the life of me update it's children. What happens is that after I update nothing changes on the screen, UNLESS I start scrolling. Once I scroll it updates perfectly.
This is my code:
public void updateDataAtIndex(final int indexToUpdate, ReadableMap updatedChild) {
if (updatedChild != null) {
if (this.data != null && this.data.size() > indexToUpdate) {
this.data.set(indexToUpdate, updatedChild);
final SPAdapter self = this;
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
System.out.println("\n@@@@@@@@@@@@@@@@@ Updating: "+indexToUpdate);
self.notifyItemChanged(indexToUpdate);
}
});
}
}
}
Here's the full adapter: https://github.com/SudoPlz/react-native-synchronous-list/blob/master/lib/android/src/main/java/com/sudoplz/rnsynchronouslistmanager/Views/List/SPAdapter.java#L109
回答1:
That was a react-native issue ¯\_(ツ)_/¯
.
I've resolved this by intercepting requestLayout
like so:
protected boolean mRequestedLayout = false;
@Override
public void requestLayout() {
super.requestLayout();
// We need to intercept this method because if we don't our children will never update
// Check https://stackoverflow.com/questions/49371866/recyclerview-wont-update-child-until-i-scroll
if (!mRequestedLayout) {
mRequestedLayout = true;
this.post(new Runnable() {
@SuppressLint("WrongCall")
@Override
public void run() {
mRequestedLayout = false;
layout(getLeft(), getTop(), getRight(), getBottom());
onLayout(false, getLeft(), getTop(), getRight(), getBottom());
}
});
}
}
FINALLY it works.. Credit goes to this unsung hero who discovered the workaround.
来源:https://stackoverflow.com/questions/49371866/recyclerview-wont-update-child-until-i-scroll