android - 毕加索没有在 ImageView 上加载图像
问题描述
我试图在 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 函数中添加了一个日志,如果我单击链接,图像就会显示在我的浏览器中。其余元素(名称和描述)正确显示。我做错了什么?
谢谢你。
解决方案
您是否在清单中定义了互联网权限?像这样,
<uses-permission android:name="android.permission.INTERNET" />
或检查网络安全配置:-
- 从 Android 9(API 级别 28)开始,明文支持默认禁用。
你可以尝试任何选项
1.尝试使用“https://”而不是“http://”来访问 URL
2.创建文件 res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
</domain-config>
</network-security-config>
像这样将此文件添加到清单中的应用程序标记下,
<application
android:networkSecurityConfig="@xml/network_security_config">
</application>
3.使用使用明文交通
像这样在清单中的应用程序标签下添加它,
<application
android:usesCleartextTraffic="true">
</application>
推荐阅读
- ruby-on-rails - 基于相对光标的分页在 Shopify api gem 中不起作用
- bash - BASH:我必须循环 20 miljon 文件并验证它们
- java - Maven 验证构建失败但 0 次失败
- admob - 我不能给用户奖励视频奖励
- c# - 在 SQL Server 中存储数百万个 100kb 的 PDF
- angular - Angular Material mat-table 不刷新数据
- r - read.dcf(path_desc) 中的 devtools 错误:开始的行“这对应于 ...”格式错误
- apache-spark - 从 CSV 读取字符串数组作为 Pyspark 中的数组
- ruby - Monkeypatch 数组并将数组元素设置为新值
- swift - 无法快速分享 pdf(仅限 WhatsApp)