Android to set cell split line for GridView

Source: Internet
Author: User

There are two ways to set a cell split line for the GridView, one to set the background picture to set the split line, and the other to customize the GridView redraw view to set the split line. The redraw method code is as follows:

 Public classLinegridviewextendsgridview{ PublicLinegridview (Context context) {Super(context); //TODO auto-generated Constructor stub    }     PublicLinegridview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicLinegridview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); } @Overrideprotected voidDispatchdraw (canvas canvas) {Super. Dispatchdraw (canvas); View LocalView1= Getchildat (0); intColumn = GetWidth ()/localview1.getwidth (); intChildCount =Getchildcount ();        Paint Localpaint; Localpaint=NewPaint ();        Localpaint.setstyle (Paint.Style.STROKE);        Localpaint.setcolor (GetContext (). Getresources (). GetColor (R.color.grid_line));  for(inti = 0;i < childcount;i++) {View Cellview=Getchildat (i); if((i + 1)% column = = 0) {canvas.drawline (Cellview.getleft (), Cellview.getbottom (), Cellview.getright (), Cellview.getbottom (),            Localpaint); }Else if((i + 1) > (ChildCount-(childCount%column))) {Canvas.drawline (Cellview.getright (), Cellview.gettop (), Cellview.getright (), Cellview.getbottom (), LocalP            aint); }Else{canvas.drawline (Cellview.getright (), Cellview.gettop (), Cellview.getright (), Cellview.getbottom (), Lo                Calpaint);            Canvas.drawline (Cellview.getleft (), Cellview.getbottom (), Cellview.getright (), Cellview.getbottom (), localPaint); }        }        if(childCount% Column! = 0){             for(intj = 0; J < (column-childcount% column); J + +) {View Lastview= Getchildat (childCount-1); Canvas.drawline (Lastview.getright ()+ Lastview.getwidth () * j, Lastview.gettop (), lastview.getright () + lastview.getwidth () *J, Lastview.getbottom (), localpaint); }        }    }}

In the dispatchDraw method, we have the boundary of each sub-view according to a certain way to draw the border, usually a lattice only need to plot two of the edges, it is important to note that the most side of the lattice needs special treatment. super .dispatchDraw(canvas); Be sure to call, otherwise the contents of the lattice (child view) will not get the opportunity to draw.

Look at the code carefully you will find this implementation is very good, but the code is not perfect, because each line of drawing we are based on the first sub-view of the width of the benchmark, if the height of a lattice and the first lattice inconsistent, then there may be dislocation. If you can make sure that each grid is evenly sized, use it directly, or else you'll need some modification. Here is the rewrite dispatchDraw , in fact I我们重写onDraw方法也可以得到相同的结果。要完全弄明白的话,就得看看FrameWork中GridView的源码了。

Android to set cell split line for GridView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.