holder.thumbnail.context
引发标题错误。使用flickr API。
override fun onBindViewHolder(holder: FlickrImageViewHolder, position: Int) {
val photoItem = photoList[position]
Log.d(TAG, ".onBindViewHolder: ${photoItem.title} --> $position")
Picasso.get(holder.thumbnail.context).load(photoItem.image)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(holder.thumbnail)
}
.kt
个完整文件
class FlickrImageViewHolder(view: View) : RecyclerView.ViewHolder(view){
var thumbnail: ImageView = view.findViewById(R.id.thumbnail)
var title: TextView = view.findViewById(R.id.title)
}
class FlickrRecyclerViewAdapter(private var photoList: List<Photo>) : RecyclerView.Adapter<FlickrImageViewHolder>() {
private val TAG = "FlickrRecyclerViewAdapt"
override fun onCreateViewHolder(parent: ViewGroup, viewtype: Int): FlickrImageViewHolder {
Log.d(TAG, ".onCreateViewHolder new view requested")
val view = LayoutInflater.from(parent.context).inflate(R.layout.browse, parent, false)
return FlickrImageViewHolder(view)
}
override fun getItemCount(): Int {
Log.d(TAG, ".getItemCount called")
return if (photoList.isNotEmpty()) photoList.size else 0
}
fun loadNewData(newPhotos: List<Photo>){
photoList = newPhotos
notifyDataSetChanged()
}
fun getPhoto(position: Int): Photo?{
return if (photoList.isNullOrEmpty()) photoList[position] else null
}
override fun onBindViewHolder(holder: FlickrImageViewHolder, position: Int) {
val photoItem = photoList[position]
Log.d(TAG, ".onBindViewHolder: ${photoItem.title} --> $position")
Picasso.get(holder.thumbnail.context).load(photoItem.image)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(holder.thumbnail)
}
}