繁体   English   中英

onBindViewHolder(RecyclerView.ViewHolder持有人,整数位置)-无法访问持有人的“有趣”-Kotlin to Java

[英]onBindViewHolder(RecyclerView.ViewHolder holder, int position) - can't access holder's 'fun' - Kotlin to Java

我有2个用于RecyclerView的类-Adapter和ViewHolder。 在用Kotlin编写的Adapter类中,我正在实现onBindViewHolder(RecyclerView.ViewHolder holder, int position)方法,并且能够访问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
    }
}}

这是我试图将Kotlin的Adapter转换为Java的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
    }
}}

这些代码段来自Android Paging代码实验室。

更换

if(itemRepo != null){
   holder.bind(itemRepo) // not possible
}

if (itemRepo != null && holder instanceof RepoViewHolder) {
   ((RepoViewHolder) holder).bind(itemRepo)
}

对于当前代码,这是不可访问的,因为在onBindViewHolder您具有RecyclerView.ViewHolder(Base Calss)引用,该引用无法访问子类的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM