Hiii伙计们,我问一个问题,为什么kotlin中的图像可能无法显示。 我将所有图像保存在drawable中,并在String资源中标识它们。 我将anko布局用于明细项目。 谢谢大家的帮助。 这个例子我的代码: 适配器:
class ItemAdapter(private var items: List<Data> ):
RecyclerView.Adapter<ItemAdapter.ItemsViewHolder>() {
override fun onBindViewHolder(holder: ItemsViewHolder, position:
Int) {
val items = items[position]
holder.bindItem(items)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
:ItemsViewHolder {
return
ItemsViewHolder(ItemView().createView
(AnkoContext.Companion.create(par
ent.context, parent)))
}
override fun getItemCount() :Int = items.size
class ItemsViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
val txtItem : TextView = itemView.find(R.id.txt_item)
val ivItem : ImageView = itemView.find(R.id.iv_item)
fun bindItem(items: Data) {
txtItem.text = items.name
Glide.with(itemView)
.load(items.image)
.into(ivItem)
}
}
我的anko布局:
class ItemView : AnkoComponent<ViewGroup> {
private lateinit var itemsAdapter: ItemAdapter
override fun createView(ui: AnkoContext<ViewGroup>) = with(ui) {
linearLayout {
orientation = LinearLayout.VERTICAL
layoutParams = LinearLayout.LayoutParams(matchParent,
wrapContent)
cardView {
cardElevation = dip(6).toFloat()
linearLayout {
gravity = Gravity.CENTER_VERTICAL
orientation = LinearLayout.HORIZONTAL
imageView {
id = R.id.iv_item
}.lparams(width = dip(70), height = dip(70)) {
margin = dip(8)
}
textView("AC Milan") {
id = R.id.txt_item
gravity = Gravity.CENTER_HORIZONTAL
}.lparams(width = wrapContent, height =
wrapContent) {
leftMargin = dip(20)
}
}.lparams(width = matchParent, height = wrapContent)
}.lparams(width = matchParent, height = dip(90)) {
margin = dip(6)
}
}
}
}