1
我有這樣的代碼 的Android LinearLayoutManager#getChildAt(INT)retuns空
public class CustomLinearLayoutManager extends LinearLayoutManager
{
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
{
super.onLayoutChildren(recycler, state);
int childCount = getChildCount();
View lastChild = getChildAt(childCount - 1);
// Do some stuff
}
}
出於某種原因,有時lastChild
是null
。怎麼會這樣?
正如評論中所述,lastChild
爲空,因爲根本沒有孩子,所以新的問題是爲什麼在調用super.onLayoutChildren(recycler, state);
後沒有孩子?
沒有孩子,'getChildCount()'返回0,'getChildAt(-1)'返回'null'。 – earthw0rmjim
@ user13好點,我會更新答案。 –