RecyclerView layout in Android instead of GridView to implement an interface similar to Alipay

Simply use the two methods common to GridView to add split lines to GridView; http://stackoverflow.com/questions/7132030/android-gridview-draw-dividers Adding split lines to GridView is to realize grid layout. It's unclear why Google didn't add a divider attribute similar to listview to GridView. Therefore, we need to add split lines ourselves. At present, there are two methods, The first is to use the Android: horizontalspacing = "1DP" and Android: verticalspacing = "1DP" attributes of GridView, and use the gap between the background color of GridView and the background color of ItemView as the dividing line. Split line rendering using spacing attribute:

This method can realize the grid segmentation line, but the disadvantage is that there is no place in the item. It displays the background color, which is not very beautiful. This is what we refer to Alipay: Alipay does, is by default the whole line, not enough line, add blank item. As shown in the figure:

No, you can also put an item placeholder to solve the background problem.

The second method is to use the background selector. Compared with the first method, the second method is simpler to implement, but it also has a small defect. Because the item uses a selector, the division line between two adjacent items is equivalent to two, which will deepen. As shown in the figure:

Look at the picture carefully. The line on the right of online recharge is not obvious compared with the line above.

Recyclerview, let's talk about today's protagonist. Regardless of the rotation chart at the top, the grid view may be the first thing you think of in the following table. However, now that we have a new and powerful recyclerview control, we can abandon listview or GridView in most scenarios. I believe you have some understanding and application of recyclerview instead of listview in the project, but you may be a little unfamiliar with the use of recyclerview instead of GridView. Next, in this article, I will tell you in detail how to use a recyclerview control to handle a home page like this.

Integrated recyclerview

First, if you have never tried recyclerview, of course, you should make the following configuration in build.gradle to integrate recyclerview

layout

Now let's simply implement the desired effect in mainactivity, then in activity_ Main.xml only needs a recyclerview. As follows:

Adapter

First, let's take a look at how the adapter is written

Item layout file:

Activity

Next, let's take a look at how we write the code in the activity

Well, let's run it and see the effect!

Well, there is still a little gap with the effect drawing, that is, draw lines! How should the line be drawn? Draw in layout file? It seems feasible, but what if the border of each item is irregular? Draw a bunch of layout files? It's too troublesome. Therefore, another feature in the recyclerview to be used here: itemdecoration.

Here we write a class named myitemdecoration, as follows:

Go back to our activity and add the following code:

Finally, let's see what the effect is:

Of course, although there are some defects in the final effect, I believe everyone has basically understood the ideas and must make corresponding adjustments when applied to their own projects.

Finally, I have to say that now recyclerview basically has the ability to replace listview and GridView. In our project knowledge, most lists are presented in the form of recyclerview control. This article only explains how to use recyclerview from a very entry-level perspective. For this effect, there are many other implementation methods, especially for the processing in the adapter. I hope to communicate with you more. thank you!

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
分享
二维码
< <上一篇
下一篇>>