2016-09-08 74 views
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 
    } 
} 

出於某種原因,有時lastChildnull。怎麼會這樣?

正如評論中所述,lastChild爲空,因爲根本沒有孩子,所以新的問題是爲什麼在調用super.onLayoutChildren(recycler, state);後沒有孩子?

+3

沒有孩子,'getChildCount()'返回0,'getChildAt(-1)'返回'null'。 – earthw0rmjim

+0

@ user13好點,我會更新答案。 –

回答

0

看來,由於某種原因,沒有意見認爲這種佈局有父母。你能發佈你的XML嗎?

相關問題