简体   繁体   English

为什么 LinearLayoutManager.childAt() 返回 null?

[英]Why does LinearLayoutManager.childAt() returns null?

I have a recylcerview where the method shown below gets called on each selection of an item.我有一个 recyclerview,其中在每次选择项目时都会调用下面显示的方法。 The method only runs sucessfully when I do not scroll the recyclerview, so the firstvisible is 0. The moment I scroll down a bit and select something the first and last visible are set correctly in the method, but as can be seen in the screenshot, for reasons I do not understand the childAt returns null instead of the view I can see on my app screen.该方法仅在我不滚动recyclerview时成功运行,因此第一个可见为0。当我向下滚动并选择第一个和最后一个可见的内容时,该方法已正确设置,但如屏幕截图所示,由于我不明白的原因, childAt 返回 null 而不是我可以在我的应用程序屏幕上看到的视图。 In the screenshot for position 7, the first 4-6 returned a child.在位置 7 的屏幕截图中,前 4-6 位返回了一个孩子。

Can someone explain to me how this can happen?有人可以向我解释这是怎么发生的吗? From my pov, getChildAt() should always return a view in this scenario.从我的观点来看, getChildAt() 在这种情况下应该总是返回一个视图。

在此处输入图像描述

first and last are going to be the adapter positions of the data and not the position as laid out in the layout manager. firstlast将是数据的适配器位置,而不是布局管理器中布置的位置。 See LinearLayoutManager#findFirstVisibleItemPosition .请参阅LinearLayoutManager#findFirstVisibleItemPosition The children will always start with zero and increase from there.孩子们总是从零开始,然后从那里增加。

That is why it works before your scroll since the child at the zeroth index in the layout manager is also the zeroth item in the adapter.这就是为什么它在滚动之前起作用的原因,因为布局管理器中第零索引处的子项也是适配器中的第零项。

Here is a discussion about the various positions in RecyclerView . 是关于RecyclerView中各种位置的讨论。

It looks like you want to make changes to all visible items.您似乎想要对所有可见项目进行更改。 Your first and last variables will have the correct start/end adapter positions that correspond to what is visible on the screen.您的firstlast变量将具有与屏幕上可见的内容相对应的正确开始/结束适配器位置。 You need the adapter positions to call the various "notify" methods.您需要适配器位置来调用各种“通知”方法。

So, given the adapter positions, we need a map to the views that are represented on the screen.因此,给定适配器位置,我们需要一个映射到屏幕上显示的视图。 As an example, the following code loops through every visible view and changes the background color of each view.例如,以下代码循环遍历每个可见视图并更改每个视图的背景颜色。

LinearLayoutManager lm = (LinearLayoutManager) Recycler.getLayoutManager();
// Get adapter positions for first and last visible items on screen.
int firstVisible = lm.findFirstVisibleItemPosition();
int lastVisible = lm.findLastVisibleItemPosition();
for (int i = firstVisible; i <= lastVisible; i++) {
    // Find the view that corresponds to this position in the adapter.
    View visibleView = lm.findViewByPosition(i);
    visibleView.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
}

If you use the child methods of the layout manager, you will need to loop from zero to LayoutManager.getChildCount() - 1 to make the changes.如果您使用布局管理器的子方法,则需要从零循环到LayoutManager.getChildCount() - 1进行更改。 You will see each attached view which, I believe, can exceed the number of visible views.您将看到每个附加视图,我相信这些视图可以超过可见视图的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM