在我使用适配器设置 RecyclerView 完成显示其项目后,是否有回调? [英] Is there a callback for when RecyclerView has finished showing its items after I've set it with an adapter?

查看:16
本文介绍了在我使用适配器设置 RecyclerView 完成显示其项目后,是否有回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个显示 RecyclerView 快速滚动条的库(此处,以防万一),我想决定何时显示以及何时隐藏快速滚动条.

I've made a library that shows a fast-scroller for RecyclerView (here, in case anyone wants), and I want to decide when to show and when to hide the fast-scroller.

我认为一个不错的决定是,如果屏幕上没有显示的项目(或者很多项目没有显示),在 RecyclerView 完成其布局过程后,我会设置快速-scroller 可见,如果所有项目都已经显示,则不需要显示.

I think a nice decision would be that if there are items that aren't shown on the screen (or there are a lot of them that do not appear), after the RecyclerView finished its layout process, I would set the fast-scroller to be visible, and if all items are already shown, there is no need for it to be shown.

我找不到 RecyclerView 的侦听器/回调来告诉我它何时完成显示项目,以便我可以检查与项目计数相比显示了多少项目.

I can't find a listener/callback for the RecyclerView, to tell me when it has finished showing items, so that I could check how many items are shown compared to the items count.

当键盘出现和隐藏时,recyclerView 也可能改变其大小.

The recyclerView might also change its size when the keyboard appears and hides itself.

滚动侦听器可能无济于事,因为它一直"发生,我只需要检查 RecyclerView 何时更改其大小或项目计数(或数据)何时更改.

The scrolling listener will probably not help, as it occurs "all the time", and I just need to check only when the RecyclerView has changed its size or when the items count (or data) has changed.

我可以用一个布局来包装 RecyclerView,通知我尺寸变化,比如我制作的这个,但我认为它不会起作用,因为 RecyclerView 可能还没有准备好告诉有多少项目是可见的.

I could wrap the RecyclerView with a layout that notifies me of size changes, like this one that I've made, but I don't think it will work as the RecyclerView probably won't be ready yet to tell how many items are visible.

检查显示的项目数量的方法可以这样使用:

The way to check the number of items being shown might be used as such:

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(layoutManager);
    ...
    Log.d("AppLog", "visible items count:" + (layoutManager.findLastVisibleItemPosition() -layoutManager.findFirstVisibleItemPosition()+1));

问题

如何在 recyclerView 完成显示其子视图时收到通知,以便我可以根据当前显示的内容决定显示/隐藏快速滚动条?

The question

How do I get notified when the recyclerView has finished showing its child views, so that I could decide based on what's currently shown, to show/hide the fast-scroller ?

推荐答案

我找到了解决这个问题的方法(感谢用户 pskink),通过使用 LayoutManager 的回调:

I've found a way to solve this (thanks to user pskink), by using the callback of LayoutManager :

final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
            @Override
            public void onLayoutChildren(final Recycler recycler, final State state) {
                super.onLayoutChildren(recycler, state);
                //TODO if the items are filtered, considered hiding the fast scroller here
                final int firstVisibleItemPosition = findFirstVisibleItemPosition();
                if (firstVisibleItemPosition != 0) {
                    // this avoids trying to handle un-needed calls
                    if (firstVisibleItemPosition == -1)
                        //not initialized, or no items shown, so hide fast-scroller
                        mFastScroller.setVisibility(View.GONE);
                    return;
                }
                final int lastVisibleItemPosition = findLastVisibleItemPosition();
                int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1;
                //if all items are shown, hide the fast-scroller
                mFastScroller.setVisibility(mAdapter.getItemCount() > itemsShown ? View.VISIBLE : View.GONE);
            }
        };

这里的好处是它运行良好,甚至可以处理显示/隐藏的键盘.

The good thing here is that it works well and will handle even keyboard being shown/hidden.

不好的是它会在不感兴趣的情况下被调用(意味着它有误报),但它不像滚动事件那样频繁,所以对我来说已经足够了.

The bad thing is that it gets called on cases that aren't interesting (meaning it has false positives), but it's not as often as scrolling events, so it's good enough for me.

稍后添加了一个更好的回调,它不会被多次调用.这是新代码,而不是我上面写的:

there is a better callback that was added later, which doesn't get called multiple times. Here's the new code instead of what I wrote above:

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
            @Override
            public void onLayoutCompleted(final State state) {
                super.onLayoutCompleted(state);
                final int firstVisibleItemPosition = findFirstVisibleItemPosition();
                final int lastVisibleItemPosition = findLastVisibleItemPosition();
                int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1;
                //if all items are shown, hide the fast-scroller
                fastScroller.setVisibility(adapter.getItemCount() > itemsShown ? View.VISIBLE : View.GONE);
            }
        });

这篇关于在我使用适配器设置 RecyclerView 完成显示其项目后,是否有回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆