Powerful Android scroll control recyclerview

The use of recyclerview is more complex than that of listview. The use of listview is five steps, while our recyclerview has seven steps, namely:

1. Add compile 'com. Android. Support. Recyclerview-v7: XX. X.x' (x is the latest version) to the dependencies closure in build.gradle of the current project. 2. Add recyclerview control to the layout and create child layout and adapter classes. 3. Create the adapter 4. Define the data source 5. Find the recylerview list control through findviewbyid 6. Set the layout manager for recylerview through setlayoutmanager(). 7. Set the adapter through setadapter()**

Let's elaborate on each step one by one using code or pictures:

**Step 1: add compile 'com. Android. Support. Recyclerview-v7: XX. X.x' (x is the latest version) to the dependencies closure in build.gradle of the current project.

Because Android defines recyclerview in the support library, if you want to use this control, you must add a corresponding dependent library. So there is the first step. Here are two ways to add library dependencies:**

1. Add directly in the file manually, and the steps are also added manually:

Remember to save after adding, and then click sync now below to synchronize. what? what? Sync now doesn't know where it is? zero

2. Open the layout file:

When the system is added, you will find that there is still nothing in the layout interface. Don't worry! At this time, drag and drop the recyclerview control from the options bar to the layout, and you will find the following interface to prove that the library dependency is added successfully. You can use the recyclerview control:

Compared with the first method, the second method is more convenient. You don't need to manually enter and know the current version, which reduces the error rate.

Step 2: add recyclerview control to the layout and create sub item layout and adapter class. In fact, we have already said the first step. Just drag the control directly to the layout interface.

-Let's go to the layout code. You will find the following code fragments in the layout code:

In the layout file, we need to create a sub item layout of recyclerview. The code is as follows:

To achieve the preview effect, we let the interface realize the picture on the left and the text on the right:

Let's create a fruit entity class, add a constructor and override the get () method:

Finally, create a class of fruitadapter adapter, let the adapter inherit recyclerview.adapter, and specify the generic type as fruitadapter.viewholder. Among them, viewholder is an internal class defined in our fruitadapter. And rewrite: oncreateviewholder(), onbindviewholder(), getitemcount(). All codes are as follows:

The above is the code of the adapter fruitadapter. The code is annotated. I believe all my friends can understand it!

The last steps are written in mainactivity. We will not explain them separately here. We will paste the code directly, so that it can be more clear at a glance. Each code has comments. I believe it is not difficult to understand:

3. Define the data source in mainactivity 4. Define the adapter 5. Find the recylerview list control through findviewbyid 6. Set the layout manager for recylerview through setlayoutmanager(). 7. Create the adapter 8. Set the adapter through setadapter()

Well, that's the basic code of recyclerview. It's for reference only. In fact, recyclerview also has click events, which I haven't written here. If the later partners need it, they can add it. In fact, this is also the code to realize the waterfall flow effect. If we want to realize the waterfall flow effect, we only need to set the sub item layout to display only pictures (waterfall flow needs to use pictures of different sizes to see better effects), and adjust the layout parameters. Set the following codes in layout management:

The effect of the operation is as follows, which can scroll up and down:

Thank you for reading. This article is for reference only. If you have any questions or errors, please point them out. Thank you.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>