我有画廊项目。 我有一个带有 rest api 的回收器视图:ImageView 和 2 个文本视图。 我想单击 small ImageView(此图像来自 rest api,它是从 Glide 下载的)并使用 Glide 在另一个片段中打开此图像(还想说它与 small 图像相同,但在全屏模式下),但我尝试使用带有导航组件的 safeargs。我尝试将此图像放入字符串值并尝试将其放入其他片段并将此图像加载到全屏片段中,但它仍然无法正常工作。
// 这是我使用 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) }
}
}
}
// 这是这个类的回收器视图适配器。我也尝试通过键和值将 Glide 图像放在那里
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)
}