gpt4 book ai didi

java - 跟着 picasso 一起画画

转载 作者:行者123 更新时间:2023-12-02 11:36:02 26 4
gpt4 key购买 nike

最近我熟悉了Picasso框架,用于从互联网下载图片。但是,问题是,据我了解,框架本身并不能完全给出我想要的东西。我有一些用于回收器 View 列表的数据类。并且必须有 Drawable,因为我正在使用类似的东西:

holder?.photoView?.setImageResource(userList[position].photoId)

在我的自定义适配器 onBindViewHolder 中。所以,我已经有了一个必须使用的属性。好吧,不要混淆,只是说我需要从中获取 Drawable 资源:

Picasso.with(context).load(url).centerCrop().fit()

或者类似的东西。预先感谢您。

编辑:

首先,我的 fragment :

fun setImg(url: String) {//Code that doesn't work
val url = userList[position].photoId
Picasso.with(context).load(url).placeHolder(R.drawable.image).into(imageView)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_hotels, container, false)

val rv = view.findViewById<RecyclerView>(R.id.hotelsView)
rv.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
val list = ArrayList<Hotel>()
list.add(Item("Milk (Just for example)", "$1.99", setImg("img")))
list.add(Hotel("Banana", "$2.99", setImg("img")))

var adapter = CustomAdapter(list)
rv.adapter = adapter

return view
}

自定义适配器:

class CustomAdapter(val userList: ArrayList<Hotel>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder?.textTitle?.text = userList[position].title
holder?.textPrice?.text = userList[position].price
//holder?.photoView?.setImageResource(userList[position].photoId)
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent?.context).inflate(R.layout.view_adapter, parent, false)
return ViewHolder(v)
}

override fun getItemCount(): Int {
return userList.size
}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
val textTitle = itemView.findViewById<TextView>(R.id.text_first)
val textPrice = itemView.findViewById<TextView>(R.id.text_second)
val photoView = itemView.findViewById<ImageView>(R.id.photoView)
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NamespaceTypo">

<android.support.v7.widget.CardView
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#FFF">
<LinearLayout
android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/photoView"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<TextView
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:id="@+id/text_first"
android:text="Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:text="Price"
android:id="@+id/text_second"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

仅此而已。

最佳答案

ImageView imageView = findViewByID(R.id.imageview);
String url = "http://www.website.com/image.png";
Picasso.with(context).load(url).placeHolder(R.drawable.image).into(imageView);

布局资源中的 XML

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

这将从互联网获取 URL 并将其放入 ImageView 中。

看起来你在适配器中...所以你可以做这样的事情...

ImageView imageView = findViewByID(R.id.imageview);
String url = userList[position].photoId;
Picasso.with(context).load(url).placeHolder(R.drawable.image).into(imageView);

旁注:您不需要占位符。当您的图像加载时, ImageView 所在的空间将是白色的,直到图像加载。

<小时/>

更多地查看您的代码...这是错误的..

list.add(Item("Milk (Just for example)", "$1.99", setImg("img")))
list.add(Hotel("Banana", "$2.99", setImg("img")))

应该是这样的。

list.add(Item("Milk (Just for example)", "$1.99", "http://www.google.com/image.jpg"))
list.add(Hotel("Banana", "$2.99", "http://www.google.com/image.jpg"))

然后在你的适配器中执行此操作...(它是在 java 中... Picasso 支持 kotlin 吗?)

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder?.textTitle?.text = userList[position].title
holder?.textPrice?.text = userList[position].price
String url = userList[position].url
Picasso.with(context).load(url).placeHolder(R.drawable.image).into(photoView);
//holder?.photoView?.setImageResource(userList[position].photoId)
}

关于java - 跟着 picasso 一起画画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com