CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.29k stars 5.15k forks source link

3.0.2版本,RV弄无限循环滚动,每次都报空指针,getItem、getDefItemCount方法都重写了 #3175

Open CordyHo opened 4 years ago

CordyHo commented 4 years ago

代码:

override fun convert(holder: BaseDataBindingHolder<RvItemDataBinding>, item: JsonBean.DataBean) {
    holder.dataBinding?.data = item
}

override fun getDefItemCount(): Int {
    return Int.MAX_VALUE
}

override fun getItem(position: Int): JsonBean.DataBean {
    return if (position >= data.size)
        data[position % data.size]
    else
        data[position]
}

错误信息: kotlin.KotlinNullPointerException at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.kt:231) at com.chad.library.adapter.base.BaseQuickAdapter.onCreateViewHolder(BaseQuickAdapter.kt:75) at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)

这是我用原生Adaoter写法,可以正常运行:

`

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
    val binding = DataBindingUtil.inflate<RvItemDataBinding>(LayoutInflater.from(context), R.layout.rv_item_data, parent, false)
    return MyVH(binding.root, binding)
}

override fun getItemCount(): Int {
    return Int.MAX_VALUE
}

override fun onBindViewHolder(holder: MyVH, position: Int) {
    if (position >= data.size)
        holder.binding.data = data[position % data.size]
    else
        holder.binding.data = data[position]
}

class MyVH(itemView: View, val binding: RvItemDataBinding) : RecyclerView.ViewHolder(itemView)

`

limuyang2 commented 4 years ago

能给个demo么?

CordyHo commented 4 years ago

能给个demo么?

链接: https://pan.baidu.com/s/1gpilUzlHbTI4aDRY-mindw 提取码: 4gte 有2个Adapter,一个原生写的,一个用BRVAH,你看看

xichunjie commented 4 years ago

我也是,getDefItemCount+1,就报空指针

zhanhon commented 4 years ago

你好,解决了吗

soul7630 commented 4 years ago

你好,解决了吗?我也遇到同样的问题了

CordyHo commented 4 years ago

你好,解决了吗

没,最新版还是空指针

CordyHo commented 4 years ago

你好,解决了吗?我也遇到同样的问题了

没,最新版还是空指针

LxzBUG commented 4 years ago

解决了吗😂😂😂

withyi9223 commented 4 years ago

需要重写getItemViewType方法呀

private val adapter by lazy { object : BaseQuickAdapter<Int, BaseViewHolder>(R.layout.item_layout) { override fun convert(helper: BaseViewHolder?, item: Int?) { helper?.itemView?.apply { textView.text = "hello world$item" } }

        override fun getItemCount(): Int {
            return Int.MAX_VALUE
        }

        override fun getItem(position: Int): Int? {
            return data[position % data.size]
        }

        override fun getItemViewType(position: Int): Int {
            var count = headerLayoutCount + data.size
            if (count <= 0) {
                count = 1
            }
            return super.getItemViewType(position % count)
        }
    }
}
CordyHo commented 4 years ago

可以了,是什么原理啊?

android-taoge commented 2 years ago

用的最新版,为啥我已经重写了这三个方法,还是报空指针?

android-taoge commented 2 years ago

而且是照着楼上粘的代码。。

artillerymans commented 2 years ago

解了吗