Lesson 4
Android Developer Fundamentals V2
1
1
1
1
User Interaction
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
4.4 RecyclerView
2
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Contents
3
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
What is a RecyclerView?
4
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
RecyclerView Components
5
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Components
Overview
6
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Components
How components fit together overview
7
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Layout Manager
What is a layout manager?
8
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Adapter
What is an adapter?
9
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Adapter
What is a ViewHolder?
10
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Implementing RecyclerView
11
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Implementation
Steps Summary
12
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
app/build.gradle
Add dependency to app/build.gradle
Add RecyclerView dependency to build.gradle if needed:
dependencies {
...
compile 'com.android.support:recyclerview-v7:26.1.0'
...
}
13
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Activity layout
Add RecyclerView 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>
14
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Item layout
Create layout for 1 list item
<LinearLayout …>
<TextView
android:id="@+id/word"
style="@style/word_title" />
</LinearLayout>
15
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 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;
}
}
16
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Adapter: onCreateViewHolder()
Adapter has 3 required methods
Let's take a look!
17
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 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);
}
18
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 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);
}
19
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Adapter: getItemCount()
getItemCount()
@Override
public int getItemCount() {
// Return the number of data items to display
return mWordList.size();
}
20
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Adapter: ViewHolder Class
Create the view holder in adapter class
class WordViewHolder extends RecyclerView.ViewHolder { //.. }
If you want to handle mouse clicks:
class WordViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener { //.. }
21
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
ViewHolder: Constructor
View holder 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
22
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Create RecyclerView
Create the RecyclerView in Activity onCreate()
mRecyclerView = findViewById(R.id.recyclerview);
mAdapter = new WordListAdapter(this, mWordList);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new
LinearLayoutManager(this));
23
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Practical: RecyclerView
24
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
Learn more
25
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
What's Next?
26
Android Developer Fundamentals V2
RecyclerView
This work is licensed under a Creative Commons Attribution 4.0 International License.
END
27
Android Developer Fundamentals V2