[英]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.