我正在将一些代码从java迁移到kotlin,我不是这两种语言的Maven。
在大多数情况下,我一直在利用AndroidStudio的translate to kotlin功能,然后检查并修复任何bug。
现在我正在迁移一个回收器视图,我有一些java代码如下所示:
Java
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((VideoPlayerViewHolder)viewHolder).onBind(vods.get(i), requestManager);
}
我特别关注上面的.onbind(vods.get(i),requestmanager),因为在代码翻译成kotlin之后,我在onbind上遇到了一个未解决的引用错误。
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, i: Int) {
(viewHolder as VideoPlayerViewHolder).onBind(vods[i], requestManager)
}
所以我想知道如何解决这个错误。Kotlin似乎没有自动处理。有人能告诉我更多关于这里发生的事情吗。这里是完整的文件也。
谢谢
Java
public class VideoPlayerRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<VOD> vods;
private RequestManager requestManager;
public VideoPlayerRecyclerAdapter(ArrayList<VOD> vods, RequestManager requestManager) {
this.vods = vods;
this.requestManager = requestManager;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new VideoPlayerViewHolder(
LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_video_list_item, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((VideoPlayerViewHolder)viewHolder).onBind(vods.get(i), requestManager);
}
@Override
public int getItemCount() {
return vods.size();
}
}
Kotlin
abstract class VideoPlayerRecyclerAdapter(
vods: ArrayList<VOD>,
requestManager: RequestManager
) :
RecyclerView.Adapter<RecyclerView.ViewHolder?>() {
private val vods: ArrayList<VOD>
private val requestManager: RequestManager
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerView.ViewHolder {
return VideoPlayerViewHolder(
LayoutInflater.from(viewGroup.context)
.inflate(R.layout.vod_item, viewGroup, false)
)
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, i: Int) {
(viewHolder as VideoPlayerViewHolder).onBind(vods[i], requestManager)
}
init {
this.vods = vods
this.requestManager = requestManager
}
}
ViewHolder
public class VideoPlayerViewHolder extends RecyclerView.ViewHolder {
FrameLayout media_container;
TextView title;
ImageView thumbnail, volumeControl;
ProgressBar progressBar;
View parent;
RequestManager requestManager;
public VideoPlayerViewHolder(@NonNull View itemView) {
super(itemView);
parent = itemView;
media_container = itemView.findViewById(R.id.media_container);
thumbnail = itemView.findViewById(R.id.thumbnail);
title = itemView.findViewById(R.id.title);
progressBar = itemView.findViewById(R.id.progressBar);
}
public void onBind(MediaObject mediaObject, RequestManager requestManager) {
this.requestManager = requestManager;
parent.setTag(this);
title.setText(mediaObject.getTitle());
this.requestManager
.load(mediaObject.getThumbnail())
.into(thumbnail);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!