I have gallery project. I have a recycler view with rest api: ImageView, and 2 Text Views. I want to click small ImageView (this image is from rest api and it's downloaded from Glide) and Open This Image In another fragment with Glide using(Also want to say it's same image as small , but in fullscreen) , but I try to use safeargs with navigation components. I try to put this image in to String value and try to get it in other fragment and load this image in Fullscreen fragment , but it's still not working.
// this is my first fragment with recyclerView
@AndroidEntryPoint
class GalleryListFragment : Fragment() {
lateinit var adapter:GalleryRecyclerView
private lateinit var binding : FragmentGalleryListBinding
private val viewmodel by viewModels<GalleryViewModel>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentGalleryListBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapter = GalleryRecyclerView(view)
binding.listforgallery.layoutManager = LinearLayoutManager(context)
binding.listforgallery.adapter = adapter
viewmodel.getPhotosGallery()
viewmodel.photostate.observe(viewLifecycleOwner) { list ->
list.body()?.let { adapter.createlistwithphotos(it) }
}
}
}
// this a recycler view adapter for this class. Also I try to put Glide image there by key and value
class GalleryRecyclerView@Inject constructor(private val view: View):RecyclerView.Adapter<GalleryRecyclerView.GalleryViewHolder>() {
var listwithelements =ArrayList<RequestPhotosResponseItem>()
class GalleryViewHolder(view:View) : RecyclerView.ViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.gallery_layout,parent,false)
return GalleryViewHolder(view)
}
override fun getItemCount(): Int = listwithelements.size
override fun onBindViewHolder(holder: GalleryViewHolder, position: Int) {
holder.itemView.findViewById<TextView>(R.id.author).text = listwithelements[position].user.username
holder.itemView.findViewById<TextView>(R.id.titleforphoto).text = listwithelements[position].alt_description
Glide.with(view).load("${listwithelements[position].urls.small}").into(holder.itemView.findViewById(R.id.imageforrest))
holder.itemView.findViewById<ImageView>(R.id.imageforrest).setOnClickListener {
val image = Glide.with(view).load("${listwithelements[position].urls.regular}")
val action = GalleryListFragmentDirections.actionGalleryListFragmentToPhotoFragment(image.toString())
view.findNavController().navigate(action)
}
}
fun createlistwithphotos(list: List<RequestPhotosResponseItem>){
listwithelements.addAll(list)
notifyDataSetChanged()
}
}
data class RequestPhotosResponseItem(
val alt_description: String,
val blur_hash: String,
val color: String,
val created_at: String,
val current_user_collections: List<Any>,
val description: String,
val height: Int,
val id: String,
val liked_by_user: Boolean,
val likes: Int,
val promoted_at: String,
val updated_at: String,
val urls: Urls,
val user: User,
val width: Int
) // This is Model class for request
data class Urls(
val full: String,
val raw: String,
var regular: String,
val small: String,
val small_s3: String,
val thumb: String
) // model class for images urls , they're named by size(also want to add that the names are correct.)
// And finally class in which I want to see fullscreen image which was getting from rest api
@AndroidEntryPoint
class FullscreenPhotoFragment : Fragment() {
private lateinit var binding : FragmentFullscreenPhotoBinding
private val args:FullscreenPhotoFragmentArgs by navArgs()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFullscreenPhotoBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// binding.button.setOnClickListener {
// findNavController().navigate(R.id.action_photoFragment_to_galleryListFragment)
// }
Glide.with(this).load(args.imageURL).into(binding.fullscreenimage)
}