[英]How to know when the Recyler view in android is done inflating items?
要求是我想在屏幕上突出顯示回收站視圖的第一項。 我正在使用 recyclerView.getChildAt(0) 來獲取第一項,但是如何確定回收器視圖的第一項是否可用? 我不能直接調用 getChildAt(),因為在某些情況下,.network 響應可能很慢,getChildAt(0) 可能會返回 null。
嘗試使用 onGlobalLayoutListener,沒有用。
為什么不在適配器的onBindViewHolder
中突出顯示它? 您可以檢查該項目是否位於 position 0 並相應地突出顯示。 無論如何,適配器負責視圖。 您無需在適配器 class 之外處理它。
override fun onBindViewHolder(holder: YourHolderType, position: Int) {
//...
if (position == 0) {
// highlight the view in the holder
} else {
// de-highlight the view
}
}
要查找總項目數(而不是子視圖),請檢查:
if (recyclerView.getAdapter().getItemCount() > 0) {
// Do something with items...
} else {
// No items, do something else?
}
如果您不確定何時進行上述檢查:
import androidx.recyclerview.widget.LinearLayoutManager;
// ...
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false){
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
// Your check should be here...
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.