简体   繁体   中英

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

I have 2 classes for RecyclerView - Adapter and ViewHolder. In Adapter class which is written in Kotlin I'm implementing onBindViewHolder(RecyclerView.ViewHolder holder, int position) method and I am able to access holder.bind() - fun - easly but when I try to remake this Kotlin file to Java file and access holder.bind() it is not possible. How can I access holder.bind() from Java file?

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 Adapter file:

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
    }
}}

And this is my Java file in which I'm trying to convert Kotlin's Adapter to 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
    }
}}

These code snippets are from Android Paging codelab.

Replace

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

with

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

With current code it is not accesible because at onBindViewHolder you have reference of RecyclerView.ViewHolder(Base Calss) which cannot access the property of child class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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