当我在8.0中覆盖LinearLayoutMangager的onMeasure方法时,为什么RecyclerView.State的itemCount为0

时间:2019-04-08 02:53:40

标签: android recyclerview-layout

我暗示一个LayoutManager继承了LinearLayoutManager,并且覆盖了onMeasure()方法,但是我发现了该方法的参数“状态”,在Android 8.0设备上其itemCount为0。 我的适配器有5个数据,在Android 7.0上,较低的设备itemCount正常。 所以我想知道为什么Android 8.0的行为会发生变化?

override fun onMeasure(recycler: RecyclerView.Recycler?, state: RecyclerView.State?, widthSpec: Int, heightSpec: Int) {
        // recyclerview's adapter has 5 data
        // on 8.0 count = 0
        // but on 7.0 and lower count = 5
        var count = state?.itemCount ?: 0
        if (count > 0) {
            // do something

        } else {
            super.onMeasure(recycler, state, widthSpec, heightSpec)
        }
    }

0 个答案:

没有答案