简体   繁体   中英

Android LinearLayoutManager#getChildAt(int) retuns null

I have this code

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
    }
}

For some reason sometimes lastChild is null . How can it be?

As mentioned in comments, lastChild is null because there is no childs at all, so new question is why there is no childs after calling super.onLayoutChildren(recycler, state); ?

It seems that for some reason, there are no views that have this layout has a parent. Can you post your xml?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM