我有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
}
}}
这是我的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
}
}}
这些代码段来自Android Paging codelab。
答案 0 :(得分:3)
替换
do {
$deliveryDate = date("j-F-Y", strtotime($deliveryDate. "+1 day"));
} while (in_array($deliveryDate, $allHolidays)
与
if(itemRepo != null){
holder.bind(itemRepo) // not possible
}
使用当前代码,它是不可访问的,因为在if (itemRepo != null && holder instanceof RepoViewHolder) {
((RepoViewHolder) holder).bind(itemRepo)
}
您引用了onBindViewHolder
,它无法访问子类的属性。