Support RecyclerView doesn't show anything until touched

前端 未结 9 1797
后悔当初
后悔当初 2021-02-04 23:41

I\'m using the support RecyclerView in my app, and I see the most bizarre thing. It doesn\'t display any items until I touch to scroll. Then, all of a sudden, the RecyclerView p

9条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 00:18

    So at first, just like everybody else I just added:

    recyclerView.smoothScrollToPosition(0)
    

    and it worked pretty much good enough, however it wasn't nice and you had to remember to add it every time. and I then I followed @SudoPlz comment and answer on another question and it also worked too, you have to extend RecyclerView and override requestLayout:

    private boolean mRequestedLayout = false;
    
    @SuppressLint("WrongCall")
    @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(() -> {
                mRequestedLayout = false;
                layout(getLeft(), getTop(), getRight(), getBottom());
                onLayout(false, getLeft(), getTop(), getRight(), getBottom());
            });
        }
    }
    

    while still, I would have preferred this to fixed after 4, 5 years, however, this was a good workaround, and you won't forget about them in your view.

提交回复
热议问题