Sie sind auf Seite 1von 27

Android Developer Fundamentals

User
Interaction
and Intuitive
Navigation
Lesson 4

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 1
Fundamentals Commercial 4.0 International Lic
4.4 Recycler View

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 2
Fundamentals Commercial 4.0 International Lic
Contents

●.RecyclerView Components
●.Implementing a RecyclerView

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 3
Fundamentals Commercial 4.0 International Lic
What is a Recycler View?
● Scrollable container for large data
sets
● Efficient
○ uses and reuses limited
number of views
○ Updates changing data fast
● RecyclerView

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 4
Fundamentals Commercial 4.0 International Lic
RecyclerVie
w
Components

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 5
Fundamentals Commercial 4.0 International Lic
Components
All Components overview
●. Data
●. RecyclerView scrolling list for list items—RecyclerView
●. Layout for one item of data—XML file
●. Layout manager handles the organization of UI components in a view—
Recyclerview.LayoutManager
●. Adapter connects data to the RecyclerView—RecyclerView.Adapter
●. View holder has view information for displaying one item—
RecyclerView.ViewHolder

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 6
Fundamentals Commercial 4.0 International Lic
How components fit together
Components
overview

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 7
Fundamentals Commercial 4.0 International Lic
LayoutisManager
What a layout manager?
●.All view groups have layout managers
●.Positions item views inside a RecyclerView.
●.Reuses item views that are no longer visible to
the user
●.Built-in layout managers include
LinearLayoutManager, GridLayoutManager, and
StaggeredGridLayoutManager
●.For RecyclerView, extend
RecyclerView.LayoutManager
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 8
Fundamentals Commercial 4.0 International Lic
Adapter
What is an adapter?
●.Helps incompatible interfaces work together, for
example, takes data from a database Cursor and
puts them as strings into a view
●.Intermediary between data and view
●.Manages creating, updating, adding, deleting item
views as the underlying data changes
●.RecyclerView.Adapter

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 9
Fundamentals Commercial 4.0 International Lic
Adapter
What is a view holder?
●.Used by the adapter to prepare one view with
data for one list item
●.Layout specified in an XML resource file
●.Can have clickable elements
●.Is placed by the layout manager
●.RecyclerView.ViewHolder

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 10
Fundamentals Commercial 4.0 International Lic
Implementin
g
RecyclerVie
w

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 11
Fundamentals Commercial 4.0 International Lic
Implementation
Steps Summary
1.Add the RecyclerView dependency to
app/build.gradle file
2.Add RecyclerView to layout
3.Create XML layout for item
4.Extend RecyclerView.Adapter
5.Extend RecyclerView.ViewHolder
6.In onCreate of activity, create a RecyclerView with
adapter and layout manager
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 12
Fundamentals Commercial 4.0 International Lic
Add dependency to
app/build.gradle
app/build.gradle
dependencies {
...
com pile 'com .android.support:recyclerview -v7:24.1.1'
...
}

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 13
Fundamentals Commercial 4.0 International Lic
Add RecyclerView
Activity layout to XML
Layout
< an d roid .su p p ort.v7.w id g et.R ecyclerV iew
android:id= "@ + id/recyclerview "
android:layout_w idth= "m atch_parent"
android:layout_height= "m atch_parent">
< /android.support.v7.w idget.RecyclerView >

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 14
Fundamentals Commercial 4.0 International Lic
Item layout
Create layout for 1 list item
< LinearLayout … >
< TextView
android:id= "@ + id/w ord"
style= "@ style/w ord_title" />
< /LinearLayout>

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 15
Fundamentals Commercial 4.0 International Lic
Adapter: Create
Implement the adapter
public class W ordListAdapter
extends RecyclerView .Adapter< W ordListAdapter.W ordView H older> {

public W ordListAdapter(Context context,


LinkedList< String> w ordList) {
m Infl
ater = LayoutInfl
ater.from (context);
this.m W ordList = w ordList;
}
}
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 16
Fundamentals Commercial 4.0 International Lic
Adapter has
Adapter: onCreateViewHolder()
3 required
methods
● .onCreateView H older()
● .inBindView H older()
● .getItem Count()

Let's take a look!

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 17
Fundamentals Commercial 4.0 International Lic
Adapter: onCreateViewHolder()
onCreateViewHolder()
@ O verride
public W ordView H older onCreateView H older(
View G roup parent, int view Type) {
// Create view from layout
View m Item View = m Infl
ater.infl
ate(
R.layout.w ordlist_item , parent, false);
return new W ordView H older(m Item View , this);
}
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 18
Fundamentals Commercial 4.0 International Lic
Adapter: onBindViewHolder()
onBindViewHolder()
@ O verride
public void onBindView H older(
W ordView H older holder,int position) {
// Retrieve the data for that position
String m Current = m W ordList.get(position);
// Add the data to the view
holder.w ordItem View .setText(m Current);
}

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 19
Fundamentals Commercial 4.0 International Lic
Adapter: getItemCount()
getItemCount()

@ O verride
public int getItem Count() {
// Return the num ber of data item s to display
return m W ordList.size();
}

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 20
Fundamentals Commercial 4.0 International Lic
Create the
Adapter: ViewHolder
view holder
Class
in
adapter class
class W ordView H older exten d s R ecyclerV iew .V iew H old er {}

If you want to handle mouse clicks:

class W ordView H older exten d s R ecyclerV iew .V iew H old er


im p lem en ts V iew .O n C lickListen er {}

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 21
Fundamentals Commercial 4.0 International Lic
ViewHolder:
View holder constructor
Constructor
public W ord V iew H old er(View item View , W ordListAdapter adapter) {
super(item View );
// G et the layout
w ord Item V iew = (TextV iew ) item V iew .f in d V iew B yId (R .id .w ord );
// Associate w ith this adapter
th is.m A d ap ter = ad ap ter;
// Add click listener, if desired
item View .setO nClickListener(this);
}
// Im plem ent onClick() if desired
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 22
Fundamentals Commercial 4.0 International Lic
Create
Createthe RecyclerView in activity's
RecyclerView
onCreate()
m RecyclerView = (RecyclerView )
fi
ndView ById(R.id.recyclerview );
m Adapter = new W ordListAdapter(this, m W ordList);
m RecyclerView .setAdapter(m Adapter);
m RecyclerView .setLayoutM anager(new
LinearLayoutM anager(this));

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 23
Fundamentals Commercial 4.0 International Lic
Practical: RecyclerView

●.This is rather complex with many separate pieces.


So, there is a whole practical where you implement
a RecyclerView that displays a list of clickable
words.

●.Shows all the steps, one by one with a complete


app
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution-Non 24
Fundamentals Commercial 4.0 International Lic
Learn more

●.RecyclerView
●.RecyclerView class
●.RecyclerView.Adapter class
●.RecyclerView.ViewHolder class
●.RecyclerView.LayoutManager class

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 25
Fundamentals Commercial 4.0 International Lic
What's Next?

● Concept Chapter: 4.4 C RecyclerView


● Practical: 4.4 P Create a Recycler View

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution-Non 26
Fundamentals Commercial 4.0 International Lic
END

Android Developer Fundamentals 27

Das könnte Ihnen auch gefallen