Presumably everyone is no longer unfamiliar with the form of list expression. There are contact lists, file lists, text message lists, etc. on your phone. This article talks about using RecyclerView to achieve list effects in Android development.
Steps for usage
Introducing RecyclerView
Add a reference in the app's build.gradle file. We are using the androidx package.
gradle:
dependencies {
// ...
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
data preparation
First decide what kind of data you want to display. Is it user information, contacts, or files. Here we take characters as an example. Before writing code, we first consider the requirements, that is, how to display and how to display the data. In daily work, there are usually UI renderings. The art design in this article was created by ourselves.
For example, display a
and 97
.
ViewHolder and layout
The data to be displayed is now determined. to design UI performance. layout is closely related to ViewHolder. Before designing the Adapter class, it would be better to write the ViewHolder class first.
Create a new layout file that defines item (list items) item_letter.xml
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
Internal classes are used here and the ViewHolder class is written in the Activity class.
Create VH class
private class VH extends RecyclerView.ViewHolder {
TextView tv1;
TextView tv2;
public VH(@NonNull View itemView) {
super(itemView);
tv1 = itemView.findViewById(R.id.tv1);
tv2 = itemView.findViewById(R.id.tv2);
}
}
As can be seen from the above, the layout of ViewHolder and item are closely related. The id in the layout is relatively simple to write. In actual projects, some more meaningful IDs can be named.
In the activity's layout file, add RecyclerView .
Add RecyclerView to layout
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/re_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Design Adapter
Design an adapter that inherits from RecyclerView.Adapter<VH>
.
Here VH
is the ViewHolder we wrote above.
LetterAdapter
holds its own list of data. 3 methods need to be implemented.
onCreateViewHolder
Method, required to return VH object.
- 这里就是创建VH对象并返回。而VH构造器要求传入一个View,我们利用`LayoutInflater`创建一个view给它。当然,创建的根据就是前面设计好的`item_letter`。
LetterAdapter:
private class LetterAdapter extends RecyclerView.Adapter<VH> {
private List<Character> dataList;
public LetterAdapter(List<Character> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter, parent, false));
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
Character c = dataList.get(position);
holder.tv1.setText(c.toString());
holder.tv2.setText(String.valueOf(Integer.valueOf(c)));
}
@Override
public int getItemCount() {
return dataList.size();
}
}
If you are careless, you may forget to initialize the dataList in the adapter. An null pointer
exception will be reported.
SetupRecyclerView
It is initialized in the onCreate method of Activity.
RecyclerView requires 2 settings, adapter and LayoutManager . The adapter is the one we ordered above. LayoutManager is used here LinearLayoutManager
, specifying the vertical direction, so that we will get a list that slides up and down.
Using LinearLayoutManager
List<Character> characterList = new ArrayList<>();
for (char c = 'a'; c <= 'z'; c++) {
characterList.add(c);
}
mLetterAdapter = new LetterAdapter(characterList);
RecyclerView letterReView = findViewById(R.id.re_view);
letterReView.setAdapter(mLetterAdapter);
letterReView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));` </pre></details>
Observe the running results
Run it on your phone or emulator and open this activity. Some friends have discovered why one sub-item on the screen takes up the entire screen?
Because the items we set earlier occupy the screen. Go back item_letter.xml
and take a look at the root layout settings. Change layout_height="match_parent"
the settings in wrap_content
. Recompile and run it and see the result.
We can also set a fixed height for the root layout. It depends on the art design and needs.
Share one last time
[Produced by Tencent Technical Team] Getting started with Android from scratch to mastering it, Android Studio installation tutorial + full set of Android basic tutorials
Android programming introductory tutorial
Java language basics from entry to familiarity
Kotlin language basics from entry to familiarity
Android technology stack from entry to familiarity
Comprehensive learning on Android Jetpack
For novices, it may be difficult to install Android Studio. You can watch the following video to learn how to install and run it step by step.
Android Studio installation tutorial
With the Java stage of learning, it is recommended to focus on video learning at this stage and supplement it with book checking and filling in gaps. If you mainly focus on books, you can type the code based on the book's explanations, supplemented by teaching videos to check for omissions and fill in the gaps. If you encounter problems, you can go to Baidu. Generally, many people will encounter entry-level problems and give better answers.
You need to master basic knowledge points, such as how to use the four major components, how to create Service, how to layout, simple custom View, animation, network communication and other common technologies.
A complete set of zero-based tutorials has been prepared for you. If you need it, you can add the QR code below to get it for free.
A complete set of basic Android tutorials