In this project, we'll be changing some of the scrollviews from previous projects to Recycler Views. This will give you more practice building up the Recycler View and less time building the foundation of the app.
For this project, I'll walk you through the transition
- Open project structure under File -> Project Structure
- Click "Dependencies"
- Click the plus under "All Dependencies" and select "Library Dependency"
- Type "recyclerview" in the search box and select "androidx.recyclerview" on the left and the latest version on the right
- Decide what information from the
ImageData
method would be useful to display in the List Activity - Build a layout to display that content called
image_item_layout
(this is up to you to decide what you think looks good, have fun and play around with views and attributes) - Give each element in this layout an id to be able to access it in the list adapter
- Create a class called
ImageListAdapter
that extendsRecyclerView.Adapter<ImageListAdapter.ViewHolder>
- Create an Inner Class called
ViewHolder
which extendsRecyclerView.ViewHolder
Use the colon ':' symbol to extend classes in Kotlin
- Add a constructor to the
ViewHolder
that accepts aView
object - Add data members to this
ViewHolder
class for each of the views in your item layout - Pass the
View
object parameter to the parent class by adding (view) to the extension statement - Attach all the views in the layout to the corresponding data members using
view.view_id
- Add a constructor to your
ImageListAdapter
that accepts aMutableList
ofImageData
objects - Override the
onCreateViewHolder
method
The signature should look like this
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageListAdapter.ViewHolder
Inflate
your item layout to aView
object usingLayoutInflater.from(context).inflate(R.layout.image_list_view, parent, false);
- Pass this
View
object to aViewHolder
constructor and return the newViewHolder
object - Override the
onBindViewHolder
method using the signatureoverride fun onBindViewHolder(holder: ImageListAdapter.ViewHolder, position: Int)
- Use the passed position parameter to get the
ImageData
object at that position of theArrayList
- Take that object and use it to populate your layout
Remember, you have already bound the elements of your layout to your
ViewHolder
object and can now access them through the passed instance of that object,holder.textImageName.text = data.getName()
Be sure to move yourOnClickListener
from theTextView
generator in yourMainActivity
to this method. Remember to usecontext.startActivity
to start the details activity
Now that we have built the components for our RecyclerView
, it's time to bolt it all together
- Change your layout xml file to remove the
ScrollView
andLinearLayout
and replace them with a singleRecyclerView
- Add data members for a
RecyclerView
,LayoutManager
(You can pick what you want, Linear, Grid, or Staggered), and yourImageListAdapter
- In your
OnCreate
method, get a handle to yourRecyclerView
- Call
setHasFixedSize(true)
on yourRecyclerView
object - Create a new
LayoutManager
of the type you chose before. Check the corresponding documentation for information on the constructors for your chosen manager - Set the
layoutManager
property of yourRecyclerView
to your newLayoutManager
- Create a new instance of your
ImageListAdapter
, passing it the list ofImageData
objects that you created last time - Set the
adapter
on yourRecyclerView
, to the instance ofImageListAdapter
Since we are going to be manually adding elements to the list, we'll have to notify the ListAdapter
when we do so.
- In your
onActivityResult
method, after adding an image to the list, callnotifyItemInserted()
on the listadapter and pass it the index of the element you just added (size - 1) - Check to make sure that if there are any other times where you edit elements in the array you are calling either
notifyItemInserted()
with the affected element ornotifyItemInserted()
without passing anything
Make sure the images you add are added to the ui and that you can select them to go into the details view
Chose another app which we have built or one you build on your own and implement a RecyclerView
in it.