【问题标题】:Android LinearLayoutManager#getChildAt(int) retuns nullAndroid LinearLayoutManager#getChildAt(int) 返回 null
【发布时间】:2017-01-16 08:38:06
【问题描述】:

我有这个代码

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。怎么可能?

正如 cmets 中提到的,lastChild 是 null 因为根本没有孩子,所以新的问题是为什么在调用 super.onLayoutChildren(recycler, state); 之后没有孩子?

【问题讨论】:

  • 没有孩子,getChildCount()返回0,getChildAt(-1)返回null
  • @user13 好点,我会更新答案。

标签: android android-recyclerview linearlayoutmanager


【解决方案1】:

似乎由于某种原因,没有具有此布局的视图具有父级。你能发布你的xml吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多