I am trying to display the gif from the link fetched from the Api but in the output, it doesn't show the gifs. It is showing the image correctly. I have used the codes already in Stack overflow but that didn't seem to work for me. Can you please help me ... Below is the code:
ViewHolderClass
class GifAdapter:RecyclerView.Adapter<GifAdapter.GifViewHolder>() {
private val items = mutableListOf<Data>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GifViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.gifsdisplaypattern,parent,false)
return GifViewHolder(view)
}
override fun onBindViewHolder(holder: GifViewHolder, position: Int) {
val currentItem = items[position]
// val avatar_url = currentItem.user.avatar_url
Glide.with(holder.itemView.context).asGif().load("https://giphy.com/embed/cBnSvKscZProc").into(holder.gif)
Log.d("Avatarurl",currentItem.embed_url)
// Glide.with(holder.itemView.context).load(currentItem.user.profile_url).into(holder.UserImage)
}
override fun getItemCount(): Int {
Log.d("Rohit", items.size.toString())
return items.size
}
fun initData(itemsList: List<Data>){
this.items.clear()
this.items.addAll(itemsList)
notifyDataSetChanged()
}
class GifViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
val gif = itemView.findViewById<ImageView>(R.id.tv_Image)
val UserImage:ImageView =itemView.findViewById(R.id.usr_img)
}
}
xml file
<androidx.cardview.widget.CardView
android:id="@ id/image"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="10dp"
>
<!-- <androidx.appcompat.widget.AppCompatImageView-->
<!-- android:id="@ id/tv_Image"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:scaleType="centerCrop"-->
<!-- />-->
<ImageView
android:id="@ id/tv_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
previously i am using currentitem.embedurl (from the data class) in place of gif link but this too didn't worked so i put the link and found no gif is displayed so i am confused where is the problem
Output
CodePudding user response:
Try this Path, current path is looks wrong in your code.
CodePudding user response:
The answer is so silly,Acutally I was directly loading the link without the .gif extension so is the problem and now it's fine so, just remember
use the appropriate extension file name
Before
Glide.with(holder.itemView.context).asGif().load("https://giphy.com/embed/cBnSvKscZProc").into(holder.gif)
After
Glide.with(holder.itemView.context).asGif().load("abc.gif").into(holder.gif)
Cheers :)