The Easy Way: Implicit Intents

You will browse to this URL first by using your old friend the implicit intent. This intent will start up the browser with your photo URL.

The first step is to make your app listen for presses on an item in the RecyclerView. Update PhotoViewHolder to pass in a lambda expression that will be invoked with the Crime’s new photoPageUri property being passed in. Invoke the lambda expression when the root view is clicked.

Listing 23.2  Firing an implicit intent when an item is pressed (PhotoListAdapter.kt)

class PhotoViewHolder(
    private val binding: ListItemGalleryBinding
) : RecyclerView.ViewHolder(binding.root) {
    fun bind(galleryItem: GalleryItem, onItemClicked: (Uri) -> Unit) {
        binding.itemImageView.load(galleryItem.url) {
            placeholder(R.drawable.bill_up_close)
        }
        binding.root.setOnClickListener { onItemClicked(galleryItem.photoPageUri) }
    }
}

Next, pass that same lambda expression into PhotoListAdapter as a constructor parameter and use it when binding a PhotoViewHolder in onBindViewHolder().

Listing 23.3  Binding PhotoViewHolder (PhotoListAdapter.kt)

class PhotoListAdapter(
    private val galleryItems: List<GalleryItem>,
    private val onItemClicked: (Uri) -> Unit
) : RecyclerView.Adapter<PhotoViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PhotoViewHolder {
        ...
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        val item = galleryItems[position]
        holder.bind(item, onItemClicked)
    }

    override fun getItemCount() = galleryItems.size
}

Finally, in PhotoGalleryFragment, pass in a lambda expression when creating an instance of PhotoListAdapter. Within that lambda expression, start an activity using an Intent containing that URL.

Listing 23.4  Starting your implicit intent (PhotoGalleryFragment.kt)

class PhotoGalleryFragment : Fragment() {
    ...
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                photoGalleryViewModel.uiState.collect { state ->
                    binding.photoGrid.adapter = PhotoListAdapter(state.images)
                    binding.photoGrid.adapter = PhotoListAdapter(
                        state.images
                    ) { photoPageUri ->
                        val intent = Intent(Intent.ACTION_VIEW, photoPageUri)
                        startActivity(intent)
                    }
                    searchView?.setQuery(state.query, false)
                    updatePollingState(state.isPolling)
                }
            }
        }
    }
    ...
}

That is it. Start up PhotoGallery and press a photo. Your browser app should pop up and load the photo page for the item you pressed (similar to the image on the left in Figure 23.1).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset