2018-11-16 13:31:21
RecyclerView
is a subclass of ViewGroup
and is a more resource-efficient way to display scrollable lists. Instead of creating a View
for each item that may or may not be visible on the screen, RecyclerView
creates a limited number of list items and reuses them for visible content.
What you’ll learn
- How to use the
RecyclerView
class to display items in a scrollable list. - How to dynamically add items to the
RecyclerView
as they become visible through scrolling. - How to perform an action when the user taps a specific item.
The relationship
I’m using another sample for this codelab, so i stored data in Room
.
Layout
list item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/text_view_style"
android:background="@android:color/holo_blue_light"/>
</LinearLayout>
main layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="moe.leer.roomwordsample.MainActivity"
android:background="@color/colorScreenBackground"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
tools:listitem="@layout/recyclerview_item"/>
</android.support.constraint.ConstraintLayout>
Adapter
ViewHolder
ViewHolder
create the item view from XML layout.
class WordViewHolder extends RecyclerView.ViewHolder {
final TextView textView;
WordViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textViewItem);
}
}
In onCreateViewHolder
, we create a view holder:
@Override
public WordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
return new WordViewHolder(inflater.inflate(R.layout.recyclerview_item, parent, false));
}
Bind view
In WordListAdapter
, we override onBindViewHolder
. In this method we init the data of the item view(like texts or images. etc.).
@Override
public void onBindViewHolder(@NonNull final WordViewHolder wordViewHolder, final int i) {
if (words != null) {
wordViewHolder.textView.setText(words.get(i).getWord());
} else {
wordViewHolder.textView.setText("No word");
}
}
Create RecyclerView in Activity
To use recycler view in activity, we should appoint its adapter and layout manager.
val adapter = WordListAdapter(this)
wordRecyclerView.adapter = adapter
wordRecyclerView.layoutManager = LinearLayoutManager(this)
Add click listener
RecyclerView
doesn’t has a click listener by default, we can implement it by ourselves.
Define a interface
The clicked position is useful for us, so let the interface get position for us.
In WordListAdapter
add:
interface OnClickListener {
void onClick(int pos);
// void onLongClick(int pos); if long click is needed
}
Add click listener
at onBindViewHolder
:
wordViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickListener.onClick(wordViewHolder.getAdapterPosition());
}
});
Usage
In activity:
adapter.setClickListener { pos ->
Toast.makeText(this, "You clicked ${adapter.getWordAtPosition(pos).word}",
Toast.LENGTH_SHORT).show()
}