![](/img/trans.png)
[英]Why does this error occur when choosing Orientation in LinearLayoutManager RecyclerView with KOTLIN
[英]Why does LinearLayoutManager.childAt() returns null?
first
和last
将是数据的适配器位置,而不是布局管理器中布置的位置。 请参阅LinearLayoutManager#findFirstVisibleItemPosition 。 孩子们总是从零开始,然后从那里增加。
这就是为什么它在滚动之前起作用的原因,因为布局管理器中第零索引处的子项也是适配器中的第零项。
这是关于RecyclerView中各种位置的讨论。
您似乎想要对所有可见项目进行更改。 您的first
和last
变量将具有与屏幕上可见的内容相对应的正确开始/结束适配器位置。 您需要适配器位置来调用各种“通知”方法。
因此,给定适配器位置,我们需要一个映射到屏幕上显示的视图。 例如,以下代码循环遍历每个可见视图并更改每个视图的背景颜色。
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));
}
如果您使用布局管理器的子方法,则需要从零循环到LayoutManager.getChildCount() - 1
进行更改。 您将看到每个附加视图,我相信这些视图可以超过可见视图的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.