如何显示 API 中的图片并在点击图片后将用户转到视频 link?科特林,改造
How to display an image from API and send the user to video link after clicking the image? Kotlin, Retrofit
我正在尝试在 recyclerview 中显示带有标题的视频缩略图
这是我的数据class:
data class PropertyItem(
val id: Int,
val thumbnailUrl: String,
val title: String,
val videoId: String,
val videoUrl: String
)
这里是 list_item xml:
<ImageView
android:id="@+id/imageViewid"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:src="@drawable/ic_launcher_background"/>
主要活动:
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var manager: RecyclerView.LayoutManager
private lateinit var myAdapter: RecyclerView.Adapter<*>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
manager = LinearLayoutManager(this)
getAllData()
}
fun getAllData(){
Api.retrofitService.getAllData().enqueue(object: Callback<List<PropertyItem>> {
override fun onResponse(
call: Call<List<PropertyItem>>,
response: Response<List<PropertyItem>>
) {
if(response.isSuccessful){
recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply{
myAdapter = MyAdapter(response.body()!!)
layoutManager = manager
adapter = myAdapter
}
}
}
override fun onFailure(call: Call<List<PropertyItem>>, t: Throwable) {
t.printStackTrace()
}
})
}
}
适配器
class MyAdapter(private val data: List<PropertyItem>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val view: View): RecyclerView.ViewHolder(view){
fun bind(propertyItem: PropertyItem){
val title = view.findViewById<TextView>(R.id.tvTitle)
val imageView = view.findViewById<ImageView>(R.id.imageViewid)
title.text = propertyItem.title
Glide.with(view.context).load(propertyItem.thumbnailUrl).centerCrop().into(imageView)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
return MyViewHolder(v)
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(data[position])
}
}
所以我从数据 class 获取所有数据,但它不显示图像以及如何将 url 添加到图像视图以便用户可以单击图像并重定向到视频?
我正在学习本教程:
https://www.codevscolor.com/android-kotlin-recyclerview-with-image
我做错了什么吗?
这段代码解决了我的问题
Picasso.with(activity)
.load(countryList[position].thumbnailUrl)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
.into(holder.thumbnailUrl)
我不得不使用 Picasso 而不是 Glide
我正在尝试在 recyclerview 中显示带有标题的视频缩略图
这是我的数据class:
data class PropertyItem(
val id: Int,
val thumbnailUrl: String,
val title: String,
val videoId: String,
val videoUrl: String
)
这里是 list_item xml:
<ImageView
android:id="@+id/imageViewid"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".3"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:src="@drawable/ic_launcher_background"/>
主要活动:
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var manager: RecyclerView.LayoutManager
private lateinit var myAdapter: RecyclerView.Adapter<*>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
manager = LinearLayoutManager(this)
getAllData()
}
fun getAllData(){
Api.retrofitService.getAllData().enqueue(object: Callback<List<PropertyItem>> {
override fun onResponse(
call: Call<List<PropertyItem>>,
response: Response<List<PropertyItem>>
) {
if(response.isSuccessful){
recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply{
myAdapter = MyAdapter(response.body()!!)
layoutManager = manager
adapter = myAdapter
}
}
}
override fun onFailure(call: Call<List<PropertyItem>>, t: Throwable) {
t.printStackTrace()
}
})
}
}
适配器
class MyAdapter(private val data: List<PropertyItem>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val view: View): RecyclerView.ViewHolder(view){
fun bind(propertyItem: PropertyItem){
val title = view.findViewById<TextView>(R.id.tvTitle)
val imageView = view.findViewById<ImageView>(R.id.imageViewid)
title.text = propertyItem.title
Glide.with(view.context).load(propertyItem.thumbnailUrl).centerCrop().into(imageView)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
return MyViewHolder(v)
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(data[position])
}
}
所以我从数据 class 获取所有数据,但它不显示图像以及如何将 url 添加到图像视图以便用户可以单击图像并重定向到视频?
我正在学习本教程: https://www.codevscolor.com/android-kotlin-recyclerview-with-image
我做错了什么吗?
这段代码解决了我的问题
Picasso.with(activity)
.load(countryList[position].thumbnailUrl)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
.into(holder.thumbnailUrl)
我不得不使用 Picasso 而不是 Glide