是否可以扩展具有伴随对象的非空构造函数的 class
Is it possible to extends a class that has a non empty constructor with a companion object
我在 Java 中有一个代码,我想将其更改为 Kotlin 语法。
jave代码是:
public class CountryDataItem (String countryNane,String countryUrl)
{
public static RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent)
{
new ViewHolder (parent);
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
private TextView countryTextView;
private ImageView countryImageView;
public ViewHolder(@NonNull View view)
{
super(view);
view.findViewById...
...
}
}
}
代码与 RecyclerView 有关。我希望能够从静态嵌套 class 类型创建尽可能多的 ViewHolder。
我写了下面的代码,但感觉我是一个糟糕的代码,不可读(我不想写匿名 class 但不知道如何写“静态” ViewHolder Class 而且总是 return同一个字段。
我写的代码:
class CountryDataItem (val countryName :String, var countryFlagUrl )
{
companion object
{
fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(parent) {
val countryNameTextView: TextView = parent.findViewById(R.id.country_name_tv)
val countryFlagUrl: ImageView = parent.findViewById(R.id.country_iv)
}
}
}
我更喜欢编写带有扩展 RecyclerView.ViewHolder class 的伴生对象的代码 自编写以来购买:
object ViewHolder: RecyclewView.ViewHolder
enforce me the provide () and argument of type View to RecyclewView.ViewHolder
我做不到
嵌套 class 在 Kotlin 中默认是静态的。您必须将它们标记为 inner
以使它们不是静态的。所以你的 Java class 在 Kotlin 中可能是这样的:
class CountryDataItem (val countryName: String, var countryFlagUrl: String) {
companion object {
fun onCreateViewHolder(parent: ViewGroup) = ViewHolder(parent)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val countryTextView: TextView = view.findViewById...
val countryImageView: ImageView = view.findViewById...
}
}
您应该在 ViewHolder class 中拥有伴随对象。像这样:
class CountryDataItem(countryName: String, countryUrl) {
class ViewHolder private constructor(view: View): RecyclerView.ViewHolder(view) {
private val textView: TextView = view.findViewById(R.id.textView)
private val imageView: ImageView = view.findViewById(R.id.imageView)
fun bind(model: Model) {
textView.text = ...
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.list_item, parent, false)
return ViewHolder(view)
}
}
}
}
现在要创建 ViewHolder,您只需调用 from
方法即可。像这样:
CountryDataItem.ViewHolder.from(parent)
我在 Java 中有一个代码,我想将其更改为 Kotlin 语法。 jave代码是:
public class CountryDataItem (String countryNane,String countryUrl)
{
public static RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent)
{
new ViewHolder (parent);
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
private TextView countryTextView;
private ImageView countryImageView;
public ViewHolder(@NonNull View view)
{
super(view);
view.findViewById...
...
}
}
}
代码与 RecyclerView 有关。我希望能够从静态嵌套 class 类型创建尽可能多的 ViewHolder。 我写了下面的代码,但感觉我是一个糟糕的代码,不可读(我不想写匿名 class 但不知道如何写“静态” ViewHolder Class 而且总是 return同一个字段。
我写的代码:
class CountryDataItem (val countryName :String, var countryFlagUrl )
{
companion object
{
fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(parent) {
val countryNameTextView: TextView = parent.findViewById(R.id.country_name_tv)
val countryFlagUrl: ImageView = parent.findViewById(R.id.country_iv)
}
}
}
我更喜欢编写带有扩展 RecyclerView.ViewHolder class 的伴生对象的代码 自编写以来购买:
object ViewHolder: RecyclewView.ViewHolder enforce me the provide () and argument of type View to RecyclewView.ViewHolder
我做不到
嵌套 class 在 Kotlin 中默认是静态的。您必须将它们标记为 inner
以使它们不是静态的。所以你的 Java class 在 Kotlin 中可能是这样的:
class CountryDataItem (val countryName: String, var countryFlagUrl: String) {
companion object {
fun onCreateViewHolder(parent: ViewGroup) = ViewHolder(parent)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val countryTextView: TextView = view.findViewById...
val countryImageView: ImageView = view.findViewById...
}
}
您应该在 ViewHolder class 中拥有伴随对象。像这样:
class CountryDataItem(countryName: String, countryUrl) {
class ViewHolder private constructor(view: View): RecyclerView.ViewHolder(view) {
private val textView: TextView = view.findViewById(R.id.textView)
private val imageView: ImageView = view.findViewById(R.id.imageView)
fun bind(model: Model) {
textView.text = ...
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.list_item, parent, false)
return ViewHolder(view)
}
}
}
}
现在要创建 ViewHolder,您只需调用 from
方法即可。像这样:
CountryDataItem.ViewHolder.from(parent)