Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

android - 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.

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("
@@@@@@@@@@@@@@@@@ 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...