【问题标题】:Getting Drawable with Picasso用毕加索变得可画
【发布时间】:2018-02-23 00:02:10
【问题描述】:

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

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

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

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

或类似的东西。 提前谢谢你。

编辑:

首先,我的片段:

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。不是可绘制的。 Picasso 使用 URL 进入 ImageView。
  • @DroiDev 但是如果我的情况需要可绘制怎么办?...
  • 你可以使用占位符...看我的回答

标签: java android kotlin drawable picasso


【解决方案1】:
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 中....毕加索支持 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)
}

【讨论】:

  • 这意味着,我需要创建空的可绘制对象并将其设置为图像视图的背景,然后才使用此代码?对不起,我只是不明白它是如何工作的(因为我发现了一些私有方法,称为 getPlaceholderDrawable() ,它让我感到困惑..)
  • 别想太多。您的 XML 可能如下所示(即将编辑)
  • 哦,我没有看到你的编辑。是的,我正在使用 Adapter for Card View,它在 Recycler View 中使用。而且,我担心,如果列表中的所有项目都将带有一张图片。也许我应该编辑我的帖子并遵循我正在使用的所有代码。当然是在合理的范围内
  • 它不会是相同的图像。我正在做一个项目。只要您将列表中的项目放在位置,就可以了。承诺。 :)
  • 嗯,不,我想我应该编辑我的问题给你我的代码。不幸的是,这段代码不适合我的情况,因为我的所有内容都在不同的类中。
猜你喜欢
  • 2016-08-24
  • 2016-10-11
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2016-04-02
  • 2016-07-25
相关资源
最近更新 更多