【发布时间】:2021-08-09 14:56:32
【问题描述】:
我正在尝试在 RecyclerView 元素中加载图像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
android:id="@+id/cardview"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/innerrl"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:id="@+id/thumbnail"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"
android:layout_toEndOf="@+id/thumbnail"
android:layout_marginStart="20dp"
android:layout_below="@+id/name"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
RecyclerView 适配器:
class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
lateinit var ctx:Context
lateinit var superHeroes:List<SuperHeroe>
fun RecyclerAdapter(listaHeroes:List<SuperHeroe>, ctx:Context){
this.superHeroes=listaHeroes
this.ctx=ctx
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return ViewHolder(layoutInflater.inflate(R.layout.superhero_layout, parent, false))
}
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
val item = superHeroes.get(position)
holder.bind(item, ctx)
}
override fun getItemCount(): Int {
return superHeroes.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val superheroName = view.findViewById(R.id.name) as TextView
val desc = view.findViewById(R.id.desc) as TextView
val avatar = view.findViewById(R.id.thumbnail) as ImageView
fun bind(superhero:SuperHeroe, context: Context){
superheroName.text = superhero.nombre
desc.text = superhero.desc
loadUrl(superhero.imagen, avatar, context)
}
fun loadUrl(url: String, target:ImageView, ctx:Context) {
println(url)
Picasso.with(ctx).load(url).into(target)
}
}
}
毕加索的版本是:
implementation 'com.squareup.picasso:picasso:2.5.2'
ImageView 中没有加载任何图像,但是我在 loadUrl 函数中添加了一个日志,如果我点击链接,图像就会显示在我的浏览器中。其余元素(名称和描述)正确显示。我做错了什么?
谢谢。
【问题讨论】: