我有 2 个 RecyclerView 类 - Adapter 和 ViewHolder。
在用 Kotlin 编写的 Adapter 类中,我正在实现 onBindViewHolder(RecyclerView.ViewHolderholder, intposition)
方法,并且我能够访问 holder.bind()
- fun
- 很容易,但是当我尝试将这个 Kotlin 文件重新制作为 Java 文件并访问 holder.bind()
时,这是不可能的。如何从 Java 文件访问 holder.bind()
?
class RepoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val name: TextView = view.findViewById(R.id.repo_name)
private val description: TextView = view.findViewById(R.id.repo_description)
private val stars: TextView = view.findViewById(R.id.repo_stars)
private val language: TextView = view.findViewById(R.id.repo_language)
private val forks: TextView = view.findViewById(R.id.repo_forks)
private var repo: Repo? = null
init {
view.setOnClickListener {
repo?.url?.let { url ->
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
view.context.startActivity(intent)
}
}
}
fun bind(repo: Repo?) {
if (repo == null) {
val resources = itemView.resources
name.text = resources.getString(R.string.loading)
description.visibility = View.GONE
language.visibility = View.GONE
stars.text = resources.getString(R.string.unknown)
forks.text = resources.getString(R.string.unknown)
} else {
showRepoData(repo)
}
}
private fun showRepoData(repo: Repo) {
this.repo = repo
name.text = repo.fullName
// if the description is missing, hide the TextView
var descriptionVisibility = View.GONE
if (repo.description != null) {
description.text = repo.description
descriptionVisibility = View.VISIBLE
}
description.visibility = descriptionVisibility
stars.text = repo.stars.toString()
forks.text = repo.forks.toString()
// if the language is missing, hide the label and the value
var languageVisibility = View.GONE
if (!repo.language.isNullOrEmpty()) {
val resources = this.itemView.context.resources
language.text = resources.getString(R.string.language, repo.language)
languageVisibility = View.VISIBLE
}
language.visibility = languageVisibility
}
companion object {
fun create(parent: ViewGroup): RepoViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.repo_view_item, parent, false)
return RepoViewHolder(view)
}
}}
Kotlin 适配器文件:
class ReposAdapter : ListAdapter<Repo, RecyclerView.ViewHolder>(REPO_COMPARATOR) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return RepoViewHolder.create(parent)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val repoItem = getItem(position)
if (repoItem != null) {
(holder as RepoViewHolder).bind(repoItem)
}
}
companion object {
private val REPO_COMPARATOR = object : DiffUtil.ItemCallback<Repo>() {
override fun areItemsTheSame(oldItem: Repo, newItem: Repo): Boolean =
oldItem.fullName == newItem.fullName
override fun areContentsTheSame(oldItem: Repo, newItem: Repo): Boolean =
oldItem == newItem
}
}}
这是我的 Java 文件,我正在尝试将 Kotlin 的适配器转换为 Java:
public class ReposAdapterJava extends ListAdapter<Repo, RecyclerView.ViewHolder> {
private static DiffUtil.ItemCallback<Repo> callback = new DiffUtil.ItemCallback<Repo>() {
@Override
public boolean areItemsTheSame(Repo oldItem, Repo newItem) {
return oldItem.getFullName().equals(newItem.getFullName());
}
@Override
public boolean areContentsTheSame(Repo oldItem, Repo newItem) {
return oldItem == newItem;
}
};
protected ReposAdapterJava() {
super(callback);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return RepoViewHolder.Companion.create(parent);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Repo itemRepo = getItem(position);
if(itemRepo != null){
holder.bind(itemRepo) // not possible
}
}}
这些代码 fragment 来自 Android Paging Codelab。
最佳答案
替换
if(itemRepo != null){
holder.bind(itemRepo) // not possible
}
与
if (itemRepo != null && holder instanceof RepoViewHolder) {
((RepoViewHolder) holder).bind(itemRepo)
}
使用当前代码,它无法访问,因为在 onBindViewHolder
处,您引用了 RecyclerView.ViewHolder(Base Calss)
,它无法访问子类的属性。
关于java - onBindViewHolder(RecyclerView.ViewHolderholder, intposition) - 无法访问holder的 'fun' - Kotlin到Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372989/