Customgridlayout of Android custom ViewGroup (I)

I wrote two articles about custom view before. This article talks about the implementation of custom ViewGroup.

We know that ViewGroup is the container class of view. The LinearLayout and relativelayout we often use are subclasses of ViewGroup. When we write the layout XML, we will tell the container (all attributes starting with layout are used to tell the container), our width (layout_width), height (layout_height), layout_gravity, etc; Therefore, the function of ViewGroup is to calculate the recommended width, height and measurement mode for childview; Determine the location of childview; Why is it just the recommended width and height instead of directly determining it? Don't forget that the width and height of childview can be set to wrap_ Content, so that only childview can calculate its own width and height.

View determines its own width and height according to the measurement value and mode passed in by ViewGroup (completed in onmeasure), and then completes its own drawing in OnDraw. The ViewGroup needs to pass in the measurement value and mode of the view to the view (completed in onmeasure), and for the parent layout of this ViewGroup, it also needs to determine its width and height in onmeasure. In addition, you need to specify the location of its childview in onlayout.

Because ViewGroup has many sub views, its whole drawing process is a little more complicated than that of view, but it still follows three steps: measure, layout and draw, which we will explain in turn. In this article, let's write a grid container similar to GridView, which is called customgridview.

Custom attribute / get attribute value

LayoutParams

ViewGroup also has a very important knowledge layoutparams. Layoutparams stores some parameter information of child views when they are added to ViewGroup. When inheriting ViewGroup class, it is generally necessary to create a new layoutparams class, just like the familiar LinearLayout in SDK LayoutParams,RelativeLayout. The layoutparams class is the same as the ViewGroup class. You can do this by creating a new layoutparams class in the ViewGroup subclass you define, which inherits from the ViewGroup LayoutParams。

Now that the new layoutparams class already exists, how can we make our customized ViewGroup use our customized layoutparams class to add child views? ViewGroup also provides the following methods for us to rewrite. We can rewrite and return our customized layoutparams object.

Measure needs to do two things in onmeasure: • calculate the measured value of childview and the mode measurechildren (width measurespec, height measurespec); measureChild(child,widthMeasureSpec,heightMeasureSpec); child. measure(WidthMeasureSpec,HeightMeasureSpec); • Set the width and height of the ViewGroup. Measure the size of the ViewGroup if layout_ Width and layout_ Height is match_ Parent or specific xxxdp, which is very simple. Directly call the setmeasureddimension () method to set the width and height of the ViewGroup. If it is wrap_ Content, which is troublesome. We need to traverse all sub views, measure each sub view, and calculate the size of the final ViewGroup according to the arrangement rules of the sub views. Note: in the first chapter on customizing view, when talking about specmode, it was said that unspecified is generally the mode passed in through the measure method, and the parent control is AdapterView. It happens to be used here.

The core of layout is to call the layout method to arrange the position of each sub view according to the left and top fields in layoutparams obtained in the measure stage.

In the draw stage, the draw ViewGroup actually calls the OnDraw method of the subclasses according to the order of the subclasses. Because we are only the container of the view, we generally do not need to draw additional decoration. Therefore, in the OnDraw method, we only need to call the OnDraw default implementation method of the ViewGroup. No need to rewrite.

Finally, define the gridadatper interface in the custom ViewGroup so that the adapter can be set up externally for the ViewGroup.

The onitemclicklistener interface is defined in the custom ViewGroup so that the click events of childview can be obtained externally.

Using a custom customviewgroup

Layout file:

grid_ item:

Java files:

The effect diagram after operation is as follows:

Change the layout:

Change again

Change again

Demo download address: http://xiazai.jb51.net/201609/yuanma/CustomGridLayout (jb51.net). rar

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