android - 如何在从 google api 请求的适配器中设置图标 url
问题描述
这是我使用的 API
因为数据看起来像下图,因为我想在持有人中设置图标:
这是具有以下内容的xml文件Circular Image
:
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/place_logo"
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/mac_img"
app:civ_border_color="@color/color_00c8f8"
app:civ_border_width="1dp"
app:civ_shadow="true"
app:civ_shadow_color="#3f51b5"/>
这是我的适配器类:
package com.example.chamatestapp
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.cuboid.cuboidcirclebutton.CuboidButton
import com.example.chamatestapp.Model.Place
import com.facebook.drawee.view.SimpleDraweeView
import com.mikhaellopez.circularimageview.CircularImageView
import java.util.*
class PlacesAdapter : RecyclerView.Adapter<PlacesAdapter.ViewHolder>() {
var placeList: MutableList<Place> = ArrayList()
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val place: Place = placeList[position]
holder.textViewName.text = place.name
place.photos?.get(0)?.photo_reference?.let {
val url =
"https://maps.googleapis.com/maps/api/place/photo?photoreference=${place.photos!![0].photo_reference}&maxwidth=600&key=my_key"
holder.placeImage.setImageURI(url)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return placeList.size
}
fun insertPlaces(body: Array<Place>) {
placeList.clear()
body.let {
placeList.addAll(it)
}
notifyDataSetChanged()
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewName = itemView.findViewById(R.id.titleTxt) as TextView
val placeImage = itemView.findViewById(R.id.placeImage) as SimpleDraweeView
val place_logo = itemView.findViewById(R.id.place_logo) as CircularImageView
}
}
这是 Place 模型:
package com.example.chamatestapp.Model
class Place {
lateinit var types: Array<String>
var business_status: String? = null
var icon: String? = null
var rating: String? = null
var photos: Array<Photos>? = null
var reference: String? = null
var user_ratings_total: String? = null
var price_level: String? = null
var scope: String? = null
var name: String? = null
var opening_hours: Opening_hours? = null
var geometry: Geometry? = null
var vicinity: String? = null
var id: String? = null
var plus_code: Plus_code? = null
var place_id: String? = null
override fun toString(): String {
return "ClassPojo [types = $types, business_status = $business_status, icon = $icon, rating = $rating, photos = $photos, reference = $reference, user_ratings_total = $user_ratings_total, price_level = $price_level, scope = $scope, name = $name, opening_hours = $opening_hours, geometry = $geometry, vicinity = $vicinity, id = $id, plus_code = $plus_code, place_id = $place_id]"
}
}
我只需要在圆形图像部分添加图标,如下图所示:
解决方案
根据我对您的问题的了解,您正在尝试将图像从 url 加载到视图中。就此而言,我还看到您正在使用 SimpleDraweeView。要使用它,你必须打电话mSimpleDraweeView.setImageURI(uri);
,你应该能够加载你的图像。如果您无法加载它,您可能需要在它之前添加一个层次结构。查看解释层次结构的这些文档。除了 Fresco,您还可以使用 picasso 或 glide 等库来为它们充气。我将举一个 Glide 的例子,你可以像这样使用它:
Glide.with(context)
.load(yourphotourl)
.apply(RequestOptions().override(yoursizewidth, yoursizeheight))
.addListener(object: RequestListener<Drawable>{
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean
): Boolean {
//handle failure
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean
): Boolean {
//file loaded correctly
return false
}
})
.into(yourphotoView)
要使用 glide,您需要将依赖项添加到应用的构建 gradle 文件中。
//glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
让我知道它是否有效!
推荐阅读
- python - 单击元素时出现Python Selenium问题
- python - django :signals 不工作 pre_save()
- python - NameError:名称“drive_service”未定义 Google API
- javascript - 将数组中的所有键值推送到新数组中,只要不同键的值(在同一个对象中)满足某个条件
- html - CSS 检查状态 - 选定状态下的目标 svg
- python - Pandas - 连接标识符中的值
- javascript - 如何总是在开始时开始 SVG 动画?
- html - 消除水平滚动
- laravel - Laravel 5.7:Eloquent orderBy 相关模型以及使用 whereHas 条件
- php - 如何通过jquery AJAX调用特定的php函数