Sunteți pe pagina 1din 21

RECYCLERVIEW

Getting to know views in a fun way


A little backstory…
• Say “Hi!” to Steve MEET STEVE
• Steve has graduated in Bachelor of Views at the
University of Android Studio, then got his Master of
TextView. Steve is a textView, but at the time same, he
can also be classified as a view.

• Steve now works at your company (app), displaying


marketing material to the user,

• Steve’s specialty is to display whatever you give him and


display it on your android phone,

• But, there is a catch, like many other who had


graduated from Bachelor of Views, he/ she must not
own the data that is handed to him, he can only display
them, not to own them,

• You should always, always see views as workers, your


minions,
How interfaces interact with activities…
TYPICAL DAY AT WORK
50%
off!!

steveOne.setText = “50% off!!!”


steveTwo.setText = “Buy one get one free!”

SteveOne

• Again, the default job description of


Steves are the boss (mainActivity) Buy one
get one
hand some data to Steves and free!
Steves display them,

• Notice it is a one way directional


communication

• Remember, the boss has NO WAY of SteveTwo


knowing what the interaction
between Steve and user’s taps, i.e.
did the user tap on the Steves?
SIGN A NEW CONTRACT

• Boss thinks to himself: “Hmmm, I need to know if the


users have tapped on the Steves so I can throw any
more annoying ads and market junk to their faces!”

• So the next day, the boss went and talk to the Steves
THE TALK
• Boss: “Look, I need you to do some
extra work for me, I need you too
tell me if a user has tapped on
your”

• The Steves: “Sure, but since this is


an extra task, you need to sign this
contract”, Steves handed the boss
a onClickhandler contract
THE CONTRACT/ INTERFACE
view.onClickLisenter
He/ she who implements this
interface must defined the function:
onClick(view V)
@override
Public void onClick(View v) {
view.onClickLisenter

if (v == steveOne) {
// Waves of ads!
}

if (v == steveOne) {
// Waves of discounts that
nobody will ever care about!
}
}

Just one last thing


boss: Boss: fine!!
steveOne.setOnClickLisener = this

You need to tell me steveTwo.setOnClickLisener = this


who I should report
this to
WORKING WITH A NEW CONTRACT
50%
onClick (View v)
Where v is steveOne off!!

SteveOne
onClick (View v)
Where v is steveTwo
Buy one
get one
free!

@override
Public void onClick(View v) {

if (v == steveOne) {
// Waves of ads!
}

if (v == steveOne) {
SteveTwo
// Waves of discounts that

nobody will ever care about! SteveTwo is reporting to boss


SteveOne
} because of this line:
}
steveOne.setOnClickLisener = this
steveTwo.setOnClickLisener
KEY LEARNING POINTS
• View should never own the data it is
displaying
• If you want views to communicate back to
you, you need to implement their interfaces
(very common pattern)
• The individual views needs to know who itself
need to report to
LET’S LOOK AT RECYCLERVIEWS…
MEET OSCAR

• Say “Hi!” to Oscar

• Like Steve, he has has graduated in Bachelor of Views at


the University of Android Studio, then got his Master of
RecyclerView, a more prestige degree

• Steve’s specialty is to display items in a list

• Like many other who had graduated from Bachelor of


Views, he/ she must not own the data that is handed to
him, he can only display them, not to own them,

• You should always, always see views as workers, your


minions,
• One day, the boss decided to throw away the Steves,
and hire in an Oscar

• Because with Oscar, he can display multiple items at


once.
You are
fired!!!
Oscar: “Sorry boss, I only work with
adapters”

Boss: “Alright, Oscar here is an array of


thing to display”

String arr = { // marketing material}

Boss: “Adapters?! How does it work?”


1. An adapter tells me where to
look for the formatting of the
individual item on the list, how to
instantiate them, and how to
populate them

2. An adapter is initialized by the


data you want to display

3. I will use the information


provided in the adapter to do
my job, which is displaying the
data

Datasource Adapter RecyclerView


Adapters need four things (or four instructions)

The design of the individual


item you want the data to
show
How the data
(The UI design of the item) Where can I find
should be
populated the design of
the item
The number of
items you want
class ViewHolder extends RecyclerView.ViewHolder
to display

@override
public void onBindViewHolder(ViewHolder holder, int position)

@override
public void onCreateViewHolder(Viewgroup parent, int viewtype)

@override
Side note, the public void getItemCount() method is the driving force for the public void getItemCount()
recyclerView. If you have 10 items in your data, but somehow you returned say
5 in that method, it will only display 5 items, the recyclerView only see the
returned integer as a source of truth in the number of items you truly have.
@override
public void onCreateViewHolder(Viewgroup parent, int viewtype) Exercise T03.07

The XML file for this view

class ViewHolder extends RecyclerView.ViewHolder


Getting a handle on the textViews so you
that you can use those handles in
onBindViewHolder

TextView ViewHolderIndex TextView ListItemNumberView


class ViewHolder extends RecyclerView.ViewHolder
Getting a handle on the textViews so you
that you can use those handles
(parameters) in onBindViewHolder

TextView ViewHolderIndex TextView ListItemNumberView


@override
public void onBindViewHolder(ViewHolder holder, int position)

Here, the video example


uses a function to bind
For example: data. But, the function
parameter holder
holder.ViewHolderIndex.setText(position) contains ViewHolderIndex
and ListItemNumberView
parameter where you can
get access to and bind
data onto.

The viewHolder you just The TextView handle you created in:
created
class ViewHolder extends RecyclerView.ViewHolder
Initialize the adapter using the data. For example:

RECAP
public GreenAdapter(int numberOfItems,
ListItemClickListener listener) {
mNumberItems = numberOfItems;
mOnClickListener = listener;
viewHolderCount = 0;
}

Recycler
Data Adapter View
(mNumberList)

Use the adapter to set the recyclerView. Remember, the recyclerView doesn’t
own the data, the adapter does. Simply put, the adapter provide the data and
the instruction on how a recyclerView should

1. Instruction on where to find the list item xml


The data you want to and instantiate
display 2. Get handles on the individual views within the
list item
3. So that you can set instructions on how the
data should bind onto the list items using
those items.

S-ar putea să vă placă și