在我滚动之前 RecyclerView 不会更新孩子 [英] RecyclerView won't update child until I scroll
问题描述
我想我已经阅读了所有类似问题的所有答案,没有一个似乎对我有用.这让我发疯了,我在这上面花了太多时间.
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.
我正在实现一个 RecyclerView
这里,我一生都无法更新它的孩子.发生的情况是,在我更新屏幕上没有任何变化后,除非 我开始滚动.一旦我滚动它就会完美更新.
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("
@@@@@@@@@@@@@@@@@ Updating: "+indexToUpdate);
self.notifyItemChanged(indexToUpdate);
}
});
}
}
}
推荐答案
那是 react-native 的问题 ¯\_(ツ)_/¯
.
That was a react-native issue ¯\_(ツ)_/¯
.
我已经通过拦截 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屋!