[英]Error while explicitly casting Android's RecyclerView's ViewHolder in Kotlin: 'This cast can never succeed'
In Android, I am working on a library to make implementation of RecyclerView
easy. 在Android中,我正在使用一个库来RecyclerView
实现。 I also want that once a developer has added this library as a dependency in app build.gradle
, adding RecyclerView
dependency in app build.gradle
shouldn't be required. 我还希望,一旦开发者已经加入这个库作为一个依赖app build.gradle
,加入RecyclerView
在依赖app build.gradle
不应要求。 This library will basically be like a wrapper on RecyclerView
. 这个库基本上就像RecyclerView
上的包装器。
I handled the view part by inflating RecyclerView
dynamically in the library. 我通过在库中动态扩展RecyclerView
来处理视图部分。 For LayoutManager
, I created a class based on Factory Design Pattern, where the developer can choose which LayoutManager
and configuration he wants. 对于LayoutManager
,我创建了一个基于Factory Design Pattern的类,开发人员可以在其中选择他想要的LayoutManager
和配置。 But I faced an issue when implementing Adapter
and ViewHolder
wrapper classes. 但是在实现Adapter
和ViewHolder
包装器类时遇到了一个问题。
Attempt #1 尝试#1
open class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
Error: Missing super type dependency (which meant RecyclerView
dependency needs to be present in app build.gradle
) 错误:缺少超级类型依赖项(这意味着RecyclerView
依赖项需要存在于app build.gradle
)
Attempt #2 尝试#2
class ViewHolder(view: View)
and wherever I was using it internally in the library, I tried casing it explicitly as viewHolder as ViewHolder
or adapter as RecyclerView.Adapter
并且无论我在库中内部使用它的什么地方,我都尝试将它显式地作为viewHolder as ViewHolder
或将adapter as RecyclerView.Adapter
viewHolder as ViewHolder
adapter as RecyclerView.Adapter
Error: This cast can never succeed. 错误:此强制转换永远不会成功。
Please guide me regarding this, how can I achieve a 100% wrapper on RecyclerView
? 请为此指导我,如何在RecyclerView
上实现100%包装?
Your library presumably depends on the RecyclerView library with the following line: 您的库可能依赖于RecyclerView库,其中包含以下行:
implementation "com.android.support:recyclerview-v7:$version"
By using implementation you are not exposing this dependency to modules that depend on your library. 通过使用实现,您不会将此依赖项暴露给依赖于库的模块。 Therefore, users of your ViewHolder class will not know about the RecyclerView.ViewHolder class and it causes the described error. 因此,您的ViewHolder类的用户将不了解RecyclerView.ViewHolder类,它会导致所描述的错误。
Try using api to propagate this dependency up the module chain: 尝试使用api在模块链上传播这种依赖关系:
api "com.android.support:recyclerview-v7:$version"
Read about the differences here or here . 在此处或此处了解差异。
Update : You can create a wrapper class and hide the property using internal visibility: 更新 :您可以创建包装器类并使用内部可见性隐藏属性:
open class ViewHolder(view: View) {
internal val recyclerHolder = RecyclerView.ViewHolder(view)
}
This is similar to solution #2 but instead of casting to RecyclerView.ViewHolder, you use the property. 这与解决方案2类似,但是您可以使用属性而不是强制转换为RecyclerView.ViewHolder。
Try this way into make viewholder class. 尝试这种方式进入make viewholder类。
class ItemViewHolder : RecyclerView.ViewHolder {
constructor(itemView: View) : super(itemView){}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.