我滚动之前,RecyclerView不会更新子级 [英] RecyclerView won't update child until I scroll
问题描述
我认为我已经阅读了所有关于所有类似SO问题的所有答案,并且没有一个似乎对我有用.这让我发疯,而我为此花了太多时间.
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.
这是我的代码:
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
推荐答案
这是本机问题¯\_(ツ)_/¯
.
我已经通过拦截requestLayout
来解决了这个问题,
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.
这篇关于我滚动之前,RecyclerView不会更新子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!