我想将这个java类转换为kotlin
package com.mkhaleghy.cinema.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mkhaleghy.cinema.R;
import com.mkhaleghy.cinema.daylist.Movie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class RecyclerAdapter<T extends Element> extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
public RecyclerAdapter(Context context) {
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 10;
}
public class ViewHolder extends RecyclerView.ViewHolder {
View itemView;
public ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
}
}
}
哈哈,真是太简单了。我使用android studio kotlin工具转换它,结果是
class RecyclerAdapter<T : Element>(context: Context) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return null
}
override fun getItemViewType(position: Int): Int {
return 0
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
s
return 10
}
inner class ViewHolder(internal var itemView: View) : RecyclerView.ViewHolder(itemView)
}
但我在RecyclerAdapter.ViewHolder上收到错误,上面写着“类RecyclerAdapter需要的一个类型参数” 谁能帮助我?
答案 0 :(得分:0)
如果您有类似的通用类:
class Adapter<T : Element>
比你必须称之为:
: RecyclerView.Adapter<Adapter<Element>.ViewHolder>()
这就是问题所在。但是你应该怎么做?最简单的想法是在单独的文件(而不是内部类)中创建ViewHolder并实现一些绑定接口。但您也可以搜索其他解决方案。
请参阅此示例:https://medium.com/@mohanmanu/generic-adapter-with-kotlin-part-i-74e191d68b0f
答案 1 :(得分:0)
删除&lt;&gt;你的问题将得到解决
class EmployeeAdapter(context: Context) : RecyclerView.Adapter<EmployeeAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItemCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
inner class ViewHolder(internal var itemView: View) : RecyclerView.ViewHolder(itemView)
}