作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我使用的API
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&type=food&key="Your_Key"
因为数据要像下面的图像,因为我想在支架中设置图标:
这是具有Circular Image
的xml文件:
<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
}
}
这是地方模型:
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中的图像加载到 View 中。为此,我还看到您正在使用SimpleDraweeView。要使用它,您必须调用mSimpleDraweeView.setImageURI(uri);
,并且应该能够加载图像。如果无法加载它,则可能需要在它之前添加一个层次结构。检查这些docs,其中解释了层次结构。除了壁画之外,您还可以使用像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'
让我知道它是否有效!
关于android - 如何从Google API请求适配器中的持有人中设置图标网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62825095/
我是一名优秀的程序员,十分优秀!