在我滚动之前,RecyclerView 不会更新 child

RecyclerView won't update child until I scroll

我想我已经阅读了几乎所有类似 SO 问题的所有答案,其中 NONE 似乎在为我工作。这让我抓狂,我在这上面花了太多时间。

我正在实施 RecyclerView here,但我终生无法更新它 children。发生的事情是,在我更新屏幕上没有任何变化之后,UNLESS 我开始滚动。一旦我滚动它更新 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);
                }
            });
        }
    }
}

这是完整的适配器: https://github.com/SudoPlz/react-native-synchronous-list/blob/master/lib/android/src/main/java/com/sudoplz/rnsynchronouslistmanager/Views/List/SPAdapter.java#L109

那是一个 react-native 问题 ¯\_(ツ)_/¯

我已经通过拦截 requestLayout 解决了这个问题:

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 
        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());
                }
            });
        }
    }

终于成功了.. 感谢 this 发现解决方法的无名英雄。