This snippet is focus on how to create a RecyclerAdapter, for so please refer to the files:
- SomeFragment.java
- CustomRecyclerAdapter.java
There you will find details. In case you need you can see the classic ListView adapter in the file ListViewAdapter.java
by example if you need to create a dropdown a.k.a AndroidSpinner. If you are looking to create an Android spinner then this tutorial should help you.
Learning how to use the adapter for the RecyclerView is very important, is an optimized adapter in comparison to the previous for the ListView. This gist assume you have an xml layout for your row, if you haven't create it yet, do it now
- Go to the
CustomRecyclerAdapter.java
and adapt what you need. - Then you have to set the adapter to the RecyclerView, you can see how to do that in the
SomeFragment.java
file
Please pay close attention to what is marked as important inside the CustomRecyclerAdapter, it will spare you some headaches.
- The layout of your
Activity
orFragment
has to have 1RecyclerView
- Create your model
- Create a list_item_[model]
- Think a way in wich you will get the data (a field inside the adapter, the adapter constructor, etc)
- Create a
class
that will be the adapter [YourModels]Adapter, but don't extend it to the adapter yet - Inside your new class create an inner
class
name it [YourModel]Holder, it has to extendsRecyclerView.ViewHolder
- The view holder created is the Java representation of the layout row in the step 2, add the constructor matching super
- Inside the holder create one variable for each view you will need to set data in the layout
- Initialilze each view inside the constructor mostly like this
foo = itemView.findViewById(R.id.bar)
- Now go back to the adapter class you create and extend it
RecyclerView.Adapter
please read the next step - The
RecyclerView.Adapter
takes 1 param between angles brackes, your view holderRecyclerView.Adapter<YourModelHolder>
- Now the class it will lint an error, press alt+enter on the red lining and select implement methods
- You should now have 3 methods, onBindViewHolder, onCreateHolder and getItemCount
- In the step 3 you design how the data is gonna be pass to the adapter, now is the time to make it happen
- Once you have solved that, got to getItemCount and make it return to the size of your list.
- Please look at the adapter below and make onCreateHolder does what it has to do, is always the same
- Then go to onBindViewHolder and set the data in the row (the holder)
- Next step is handling clicks
- Create an
interface
add there 1 method that has to have as arguments the param you need to pass when the row is clicked - Make a field inside the adapter that is your interface
private ClickListener listener
- Add that field in the constructor
- Wherever you are instancing the adapter it has to implements the interface
YourActivity extends AppCompatActivity implements YourInterface
- Find the
RecyclerView
- Set a
LayoutManager
to theRecyclerView
commonlyLinearLayoutManager
(is notLinearLayout
) - Instance the adapter by example
YourModelsAdapter adapter = new YourModelsAdapter(this)
,this
is the implemented interface - Set the adapter to the
RecyclerView
- Add this gist to your favorites
- Layout of the
Activity
orFragment
is wrong - Layout of the row list_item_[model] is wrong, is usually width
match_parent
and heightwrap_content
- You haven't set the
LayoutManager
getItemCount
is wrong- You haven't implement the interface
- The adapter data doesn't exist
Very interesting about positions https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html