Support RecyclerView doesn't show anything until touched

前端 未结 9 1798
后悔当初
后悔当初 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.

    0 讨论(0)
  • 2021-02-05 00:18

    Since this is still happening, here is an specific fixed instance in case it helps someone:

    • It happened only when the cell view contains a WebView.

    The fix:

    • Call setIsRecyclable(false) from the ViewHolder
    0 讨论(0)
  • 2021-02-05 00:18

    You would have to run the stuff you do in onPostExecute on the UI thread so that the RecyclerView gets redrawn. That's why the smooth scrolling works, because that is running on the UI thread and therefore causing the view to redraw.

    0 讨论(0)
  • 2021-02-05 00:24

    It's most likely because you're not calling the correct notification methods of RecyclerView.Adapter. You have a much more granular interface for this than you previously had in ListAdapters. For example, in addAll() you should call notifyItemRangeInserted(oldConversationsSize, conversations.size()) instead of notifyDataSetChanged

    0 讨论(0)
  • 2021-02-05 00:26

    If anyone else is having this issue using RxJava and Retrofit, I solved this issue by adding the .observeOn(AndroidSchedulers.mainThread()) operator into my method chain before subscribing. I had read this was taken care of by default, and thus not explicitly necessary, but I guess not. Hope this helps.

    Example:

    public void loadPhotos() {
        mTestPhotoService.mServiceAPI.getPhotos()
                                     .subscribeOn(Schedulers.io())
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribe(photoList -> mRecyclerActivity.OnPhotosLoaded(photoList));
    }
    
    0 讨论(0)
  • 2021-02-05 00:26

    This is a bit late, but in my case I had a recylerview inside a viewpager (TabView) that was inside another viewpager (using BottomNavigationView) and smooth scrolling didn't change anything.

    However, I noticed that I had the recylerview set with Visibility GONE when the layout was drawn, so the solution was:

    1. Setting the recyclerview to Visibility VISIBLE on xml layout (so it's draw completely when the TabView is set);
    2. When the adapter is set (linked to observable with room), i do:
    new Handler(Looper.getMainLooper()).postDelayed(() -> {
    
         binding.recyclerView.scrollToPosition(0);
    
    }, getResources().getInteger(android.R.integer.config_shortAnimTime));
    
    
    0 讨论(0)
提交回复
热议问题