There are two types of intents: Explicit and Implicit. The following examples and info come from this codelab.
Explicit intents are generally used to start another activity within your app. You can construct it with the constructor that takes the Context and the Activity class as parameters. Use Intent.putExtra to send parameters to the new activity. For example:
val intent = Intent(context, DetailActivity::class.java)
Implicit intents are generally used to start an activity of the user’s choosing. For example you may have several browsers on your device that could be used to open a URL. You can construct it with the constructor that takes the Intent type and the Uri. For example:
val query = Uri.parse(DetailActivity.SEARCH_PREFIX + item)
val intent = Intent(Intent.ACTION_VIEW, query)
Create a new layout file with a LinearLayout at the top level if you plan to display multiple Views for each item. If you will only have one view, i.e. a TextView, you can make that your top level View instead. Give it an id and set the layout_width and layout_height to “wrap_content”.
MyAdapter extends RecyclerView.Adapter<MyViewHolder>
Once you have created the Adapter class, go ahead and create the inner ViewHolder class so the implemented methods will have the correct type. Then implement the Adapter classes:
onCreateViewHolder – Inflate the xml layout, and use it to create and return an instance of your ViewHolder.
val adapterLayout = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false)
onBindViewHolder – Get the item from the cached list of items, and use it to set the content of the View inside the ViewHolder (be sure to handle a null list of items).
getItemCount – Return the size of the cached list of items – handle null case.
MyViewHolder extends RecyclerView.ViewHolder. This can be an inner class of the Adapter. The constructor takes a View as a parameter. This is the layout that gets inflated by the adapter. Get any Views from the parent and store them as a member variables. You can call findViewById on the parent View to get its children.
onCreate – Set up the RecyclerView
// Initialize data.
val myDataset = Datasource().loadAffirmations()
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.adapter = ItemAdapter(this, myDataset)
RecyclerView recyclerView = findViewById(R.id.recyclerview);
// a layout manager must be provided for the RecyclerView to function
In your app build.gradle file, add the following lines to the “android” section:
viewBinding = true
In your main class, create a member variable called, “binding”, where the name of the binding class is the name of the layout converted to camelCase:
lateinit var binding: ActivityMainBinding
In onCreate(), initialize the view binding:
binding = ActivityMainBinding.inflate(layoutInflater)
Call setContentView with “binding.root” instead of the main layout file:
Now you can access views using the binding object like this, where the name of the view is its id converted to camelCase.: