Please do not mark it as 'duplicate' and Close the question simply by attaching any other question. My context is different from the suggested answer. I need an answer for Kotlin, I want to display images in an Image Slider on recyclerView. Thanks for the understanding and not for closing it. At least others who are willing to help can help me with this. I have tried myself a lot, more than a week, refer a lot of tutorials, videos on YouTube and after that only I asked here on Stackoverflow.
I wanted to add an image carousel/image slide in my app and I have been trying to do it for more than a week.
I always failed in getting the array of images (SlideModel). Every time I check the size of my array, I found it's zero. I know it's because of the time taken to fetch details from the Firestore and I do not know how to handle it.
I went through many tutorials including YouTube videos on Image Slide, but their scenario is different from mine. I have the imageslider in my recyclerView and my images are in the Firebase Storage and the links are in the Firestore and I am using Kotlin. I have managed to show the images in an ImageView, fetch other details and show them in the recyclerView. I could also show the images in the `imageSlider when the images are in the drawable.
I tried using the following image slider.
implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0'
I tried many different ways, but nothing really makes sense and that is why I am not adding my code here. Thanks in advance for your help.
Edit:
I have function getImagesForSlider() in my DashboardFragment.kt that will be called from the DashboardItemsListAdapter.kt
class DashboardFragment : BaseFragment() {
val remoteImages = ArrayList<SlideModel>()
private lateinit var binding: FragmentDashboardBinding
fun getImagesForSlider(productId: String): ArrayList<SlideModel> {
Log.d("Tag", "Called getImagesForSlider function")
Log.d("Tag", "product id is $productId")
val mFireStore = FirebaseFirestore.getInstance()
mFireStore.collection("products")
.document(productId)
.get()
.addOnSuccessListener { document ->
val numOfImages= document.getLong("image_count")?.toInt()
Log.d("Tag", "Number of images is $numOfImages")
when {
numOfImages == 1 -> {
remoteImages.add(
SlideModel(
document.getString("image"),
document.getString("title"),
ScaleTypes.FIT
)
)
}
numOfImages == 2 -> {
remoteImages.add(
SlideModel(
document.getString("image1"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image2"),
document.getString("title"),
ScaleTypes.FIT
)
)
}
numOfImages == 3 -> {
remoteImages.add(
SlideModel(
document.getString("image1"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image2"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image3"),
document.getString("title"),
ScaleTypes.FIT
)
)
}
numOfImages == 4 -> {
remoteImages.add(
SlideModel(
document.getString("image1"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image2"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image3"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image4"),
document.getString("title"),
ScaleTypes.FIT
)
)
}
numOfImages == 5 -> {
remoteImages.add(
SlideModel(
document.getString("image1"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image2"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image3"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image4"),
document.getString("title"),
ScaleTypes.FIT
)
)
remoteImages.add(
SlideModel(
document.getString("image5"),
document.getString("title"),
ScaleTypes.FIT
)
)
}
else -> {
Log.d("Tag", "There are no images")
}
}
}
return remoteImages
}
}
Following is the part of my DashboardItemsListAdapter.kt where the above function will be called.
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val model = list[position]
if (holder is MyViewHolder) {
val remoteImages =DashboardFragment().getImagesForSlider(model.product_id)
holder.itemView.image_slider.setImageList(remoteImages,ScaleTypes.FIT)
I don't know if my entire code is a blunder.
My database is as below.
