0% found this document useful (0 votes)
11 views27 pages

04.5 RecyclerView

The document provides an overview of RecyclerView, a component in Android for displaying large datasets in a scrollable format. It details the essential components of RecyclerView, including the layout manager, adapter, and view holder, along with implementation steps. The document also includes practical guidance for creating a RecyclerView in an Android application.

Uploaded by

radji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

04.5 RecyclerView

The document provides an overview of RecyclerView, a component in Android for displaying large datasets in a scrollable format. It details the essential components of RecyclerView, including the layout manager, adapter, and view holder, along with implementation steps. The document also includes practical guidance for creating a RecyclerView in an Android application.

Uploaded by

radji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Android Developer Fundamentals V2

User
Interaction

Lesson 4

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 1
Fundamentals V2 International License
4.4 RecyclerView

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 2
Fundamentals V2 International License
Contents

● RecyclerView Components
● Implementing a RecyclerView

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 3
Fundamentals V2 International License
What is a RecyclerView?
● RecyclerView is scrollable
container for large data sets
● Efficient
○ Uses and reuses limited
number of View elements
○ Updates changing data fast

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 4
Fundamentals V2 International License
RecyclerVie
w
Components

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 5
Fundamentals V2 International License
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
● ViewHolder has view information for displaying one item—
RecyclerView.ViewHolder

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 6
Fundamentals V2 International License
How components fit together
Components
overview

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 7
Fundamentals V2 International License
LayoutisManager
What a layout manager?
● Each ViewGroup has a layout manager
● Use to position View items inside a RecyclerView
● Reuses View items that are no longer visible to the user
● Built-in layout managers
○ LinearLayoutManager
○ GridLayoutManager
○ StaggeredGridLayoutManager
● Extend RecyclerView.LayoutManager

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 8
Fundamentals V2 International License
Adapter
What is an adapter?
● Helps incompatible interfaces work together
○ Example: Takes data from database Cursor and prepares
strings to put into a View
● Intermediary between data and View
● Manages creating, updating, adding, deleting View
items as underlying data changes
● RecyclerView.Adapter

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 9
Fundamentals V2 International License
Adapter
What is a ViewHolder?
● 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 4.0 10
Fundamentals V2 International License
Implementin
g
RecyclerVie
w

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 11
Fundamentals V2 International License
Implementation
Steps Summary
1. Add RecyclerView dependency to build.gradle if
needed
2. Add RecyclerView to layout
3. Create XML layout for item
4. Extend RecyclerView.Adapter
5. Extend RecyclerView.ViewHolder
6. In Activity onCreate(), create RecyclerView with
adapter and layout manager This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 12
Fundamentals V2 International License
Add dependency to
app/build.gradle
app/build.gradle
Add RecyclerView dependency to build.gradle if
needed:

dependencies {
...
compile 'com.android.support:recyclerview-
v7:26.1.0'
...
}
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 13
Fundamentals V2 International License
Add RecyclerView
Activity layout to XML
Layout
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 14
Fundamentals V2 International License
Item layout
Create layout for 1 list item
<LinearLayout …>
<TextView
android:id="@+id/word"
style="@style/word_title" />
</LinearLayout>

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 15
Fundamentals V2 International License
Adapter: Create
Implement the adapter
public class WordListAdapter
extends
RecyclerView.Adapter<WordListAdapter.WordViewHolder> {

public WordListAdapter(Context context,


LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
this.mWordList = wordList;
}
} This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 16
Fundamentals V2 International License
Adapter has
Adapter: onCreateViewHolder()
3 required
methods
● onCreateViewHolder()
● inBindViewHolder()
● getItemCount()

Let's take a look!

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 17
Fundamentals V2 International License
Adapter: onCreateViewHolder()
onCreateViewHolder()
@Override
public WordViewHolder onCreateViewHolder(
ViewGroup parent, int viewType) {
// Create view from layout
View mItemView = mInflater.inflate(
R.layout.wordlist_item, parent, false);
return new WordViewHolder(mItemView, this);
}
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 18
Fundamentals V2 International License
Adapter: onBindViewHolder()
onBindViewHolder()
@Override
public void onBindViewHolder(
WordViewHolder holder, int position) {
// Retrieve the data for that position
String mCurrent =
mWordList.get(position);
// Add the data to the view
holder.wordItemView.setText(mCurrent);
}
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 19
Fundamentals V2 International License
Adapter: getItemCount()
getItemCount()

@Override
public int getItemCount() {
// Return the number of data items to display
return mWordList.size();
}

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 20
Fundamentals V2 International License
Create the
Adapter: ViewHolder
view holder
Class
in
adapter class
class WordViewHolder extends RecyclerView.ViewHolder
{ //.. }

If you want to handle mouse clicks:

class WordViewHolder extends RecyclerView.ViewHolder


implements View.OnClickListener
{ //.. }
This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 21
Fundamentals V2 International License
ViewHolder:
View holder constructor
Constructor
public WordViewHolder(View itemView, WordListAdapter
adapter) {
super(itemView);
// Get the layout
wordItemView = itemView.findViewById(R.id.word);
// Associate with this adapter
this.mAdapter = adapter;
// Add click listener, if desired
itemView.setOnClickListener(this);
}
// Implement onClick() if desired This work is licensed under a
Android Developer RecyclerView Creative Commons Attribution 4.0 22
Fundamentals V2 International License
Create
Createthe RecyclerView in Activity
RecyclerView
onCreate()
mRecyclerView =
findViewById(R.id.recyclerview);
mAdapter = new WordListAdapter(this,
mWordList);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new

LinearLayoutManager(this));
RecyclerView
This work is licensed under a
Android Developer Creative Commons Attribution 4.0 23
Fundamentals V2 International License
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 4.0 24
Fundamentals V2 International License
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 4.0 25
Fundamentals V2 International License
What's Next?

● Concept Chapter: 4.5 RecyclerView


● Practical: 4.5 RecyclerView

This work is licensed under a


Android Developer RecyclerView Creative Commons Attribution 4.0 26
Fundamentals V2 International License
END

Android Developer Fundamentals V2 27

You might also like