Explain in detail that recyclerview sets the length and width of the background picture to be the same (take gridlayoutmanager as an example)

In the process of using recyclerview, because layoutmanager is set, the control (background) often cannot match by specifying the length and width_ parent、wrap_ Content to achieve the same length, width and size.

Problems faced:

Take the specified GridLayout (horizental) layout as an example: the actual width of the control is subject to the division of a row into several columns. Roughly speaking, width = recyclerview width ÷ number of columns. Because this process is determined at runtime, the length does not know the exact value of the width in advance, which will cause the mismatch between length and width (as shown in the figure)

The width of the logo in the figure is strictly limited to the width of each small cell of the GridLayout, and the length (without the restriction of the parent control) is the initial value.

The initial value here has two meanings:

① Certain values such as "xxdp" are specified in the layout file.

② The length is specified as "wrap_content" -- when the background is a vector graph, the length is the Android: height determined in the corresponding drawable file; When the background is a dot matrix, the length is the width of the resolution of the image.

This makes us see that the actual effect is either pulled into a thin and tall long bamboo pole or compressed into a short white gourd.

Of course, we can get the width of the control during debugging, and then specify it as the length of the logo. In this way, it does seem that the length and width are equal on the debugging machine, but does this really solve the fundamental problem?

Our software should run on the screen with multiple resolutions. The rigid specified length will inevitably make the length and width unbalanced in some models.

Therefore, the solution to this problem is to determine the length according to the actual width of the logo, so that height = width.

How to find the width?

Next is how to get the width.

According to the above formula, width = width of recyclerview ÷ number of columns, and recyclerview width = gridlayoutmanager. Getwidth(); Number of columns = gridlayoutmanage. Getspancount();

We can easily get width = gridlayoutmanager. Getwidth() / gridlayoutmanager. Getspancount();

Of course, in order to get the gridlayoutmanager instance, we need to pass it as a parameter in the recycleradapter constructor:

The caller code of recyclerview is as follows:

Next, specify the length of the logo in the recycleradapter as this value:

By the way, the layoutparams class is mentioned here. This class explains the size information it wants to the parent layout by specifying width \ height. The parent layout will meet it as much as possible according to this information.

Well, in this way, we succeeded in making the logo equal in length and width!

There is another thing

You think it's over? Did you forget something?

Let's take a look at the actual effect of the above settings:

Hey, hey! Although the effect is improved, why is it still rectangular?!

Calm down and think carefully. Is the width we get really the width of the logo?

The value calculated just now looks like distance ①. Hey!

When designing the layout, we often need to set margin and padding for the controls to keep a certain distance from each other. Of course, we should also consider this factor when obtaining the width!

The specific code for obtaining the value of margin is as follows:

We need to explicitly convert the layoutparams instance to marginlayoutparams to obtain the margin value as a member variable.

Get the margin value here. Refer to this article: http://blog.csdn.net/yunxiaoxiaoyun/article/details/22314407 Click to open the link

Get the padding code as follows (default paddingleft = = paddingright):

The comprehensive code is as follows:

Now the problem is completely solved! Let's observe the results:

PS: I almost forgot to say that I must pay attention!

Note that my call order is to call setadapter () after setlayoutmanager ().

Changing the order of two statements will invalidate the set length!

The specific mechanism is not thoroughly studied. I guess the reason is that the length and width of each control will be measured and determined again during setlayoutmanager(), overwriting the previous settings.

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