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
- Go to your
Module
'sbuild.gradle
file and add theRecyclerView
dependencyimplementation 'com.android.support:recyclerview-XXXXXXX
- Replace the
XXXXXXX
with whatever version appcompat library you are using.
Example if the appcompat line is
implementation 'com.android.support:appcompat-v7:28.0.0-rc01
, usev7:28.0.0-rc01
for theRecyclerView
- 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
thatextends
RecyclerView.Adapter<ImageListAdapter.ViewHolder>
- Create an Inner Class called
ViewHolder
whichextends
RecyclerView.ViewHolder
- Add data members to this
ViewHolder
class for each of the views in your item layout - Add a constructor to the
ViewHolder
that accepts aView
object - In the constructor, call the super constructor passing the view object to it with
super(view);
- Attach all the views in the layout to the corresponding data members using
view.findViewById
- In the
ImageListAdapter
class, add a data member for anArrayList
ofImageData
objects and one for aContext
object - Write a constructor method which accepts and stores the
ArrayList
member - Override the
onCreateViewHolder
method
The signature should look like this
public ImageListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
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 signaturepublic void onBindViewHolder(ImageListAdapter.ViewHolder holder, int position)
- Use the passed position parameter to
get
theImageData
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.setText(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, attach 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 - Call
setLayoutManager
on yourRecyclerView
, passing it your newLayoutManager
- Create a new instance of your
ImageListAdapter
, passing it theArrayList
ofImageData
objects that you created last time - Call
setAdapter
on yourRecyclerView
, passing it your instance ofImageListAdapter
Since we are going to be manually adding elements to the ArrayList
, 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.