如何知道什么时候RecyclerView已完成铺设的项目? [英] How to know when the RecyclerView has finished laying down the items?

查看:583
本文介绍了如何知道什么时候RecyclerView已完成铺设的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView这是一个CardView内。该CardView有500dp的高度,但我想缩短这个高度,如果RecyclerView较小。
所以,我不知道是否有当RecyclerView完成奠定了其项目首次被调用的任何监听器,从而可以在RecyclerView的高度设置为CardView的高度(如果小于500dp)。

I have a RecyclerView that is inside a CardView. The CardView has a height of 500dp, but I want to shorten this height if the RecyclerView is smaller. So I wonder if there is any listener that is called when the RecyclerView has finished laying down its items for the first time, making it possible to set the RecyclerView's height to the CardView's height (if smaller than 500dp).

推荐答案

我还需要后,我的看法回收充气完毕所有元素执行code。我试着在 onBindViewHolder 检查我的适配器,如果位置是最后一个,然后通知观察者。但在这一点上,回收观点仍然没有完全填充。

I also needed to execute code after my recycler view finished inflating all elements. I tried checking in onBindViewHolder in my Adapter, if the position was the last, and then notified the observer. But at that point, the recycler view still was not fully populated.

由于 RecyclerView 器具的ViewGroup 这anwser 是非常有益的。你只需要添加一个<一个href=\"http://developer.android.com/intl/ko/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html\"相对=nofollow> OnGlobalLayoutListener 到recyclerView:

As RecyclerView implements ViewGroup, this anwser was very helpful. You simply need to add an OnGlobalLayoutListener to the recyclerView:

View recyclerView=findViewById(R.id.myView);
      recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //At this point the layout is complete and the 
                    //dimensions of recyclerView and any child views are known.
                }
            });

这篇关于如何知道什么时候RecyclerView已完成铺设的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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