Android Custom ViewGroup Implementation table display student information

Source: Internet
Author: User

Some time ago, a friend of Android just wanted to implement a table to show the information, so we can extend the viewgroup to achieve a simple.

This article through the extension of Android ViewGroup implementation table can be used for course information, Student Information view display, the implementation of the table is available in a patchwork layout can also be customized viewgroup way to achieve.


The final effect is as follows:



Start by creating a basic model and activity

public class Student {/** *  */public Student () {//TODO auto-generated constructor stub}public String stuid;public Str ing stuname;public string stufrom;public string Sturoom;public string Stuclass;public string studate;}

public class Studentinfoactivity extends Activity {public studentinfoactivity () {} @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_student); Studentinfoview Courseinfoview = (studentinfoview) Findviewbyid (R.id.myview); arraylist<student> list = new arraylist<student> (); addlist (list); courseinfoview.addchildviews (list);} private void Addlist (arraylist<student> list) {Student c = new Student (); c.stuid = "stu1001"; c.stuname = "Sail"; c.stuf rom = "Zhejiang"; c.studate = "2014-10-09"; c.sturoom = "NO2105"; c.stuclass = "First Grade 1 class"; List.add (c); c = new Student (); c.stuid = "stu1 002 "; c.stuname =" Wangqing "; c.stufrom =" Hubei "; c.studate =" 2014-11-11 "; c.sturoom =" NO2012 "; c.stuclass =" First Grade 1 class "; List.add (c); C = New Student (); c.stuid = "stu1003"; c.stuname = "Li mi"; c.stufrom = "Northeast"; c.studate = "2014-11-10"; c.sturoom = "NO1901"; c.stucl The "First grade Class 2"; List.add (c); c = new Student (); c.stuid = "stu1004 "; c.stuname =" Kun "; c.stufrom =" Beijing "; c.studate =" 2014-11-12 "; c.sturoom =" NO1204 "; c.stuclass =" First Grade 3 class "; List.add (c);}} 

Layout file

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " android:background= "#ffffff" > <textview android:id= "@+id/title" android:layout_margintop= "        5DP "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text=" learners ' basic Information " Android:textsize= "18SP" android:textcolor= "#000000" android:layout_centerhorizontal= "true"/&gt    ;        <com.birds.mobile.course.studentinfoview android:id= "@+id/myview" android:layout_below= "@+id/title" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" > </com.birds.mobile.cours E.studentinfoview> </RelativeLayout>

The following highlights the extended ViewGroup class, Studentinfoview.java

Inside each lattice is a textview used to display text, a behavior of a student information, including 6 fields so there are 6 columns.

     int itemwidth = 0;     int itemheight = 0;     @Override    protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {    int w = getdefaultsize (0, WIDTHMEASURESPEC);    int h = getdefaultsize (0, heightmeasurespec);    int m = W/colcount;    Itemwidth = m;    ItemHeight = M/4;    int itemspecwidth = Measurespec.makemeasurespec (Itemwidth, measurespec.exactly);    int itemspecheigh = Measurespec.makemeasurespec (ItemHeight, measurespec.exactly);    LOG.D ("", "Get Item Width:" + itemspecwidth + ";" + W + ";" + h);    LOG.D ("", "H:" + itemheight + "width:" + m);    Measurechildren (Itemspecwidth, itemspecheigh);    Setmeasureddimension (W, h);    }

public int colcount = 6;//Six Columns

Height we take the width of 1/4, we can adjust ourselves, we put the width and height through the width of the entire viewgroup calculation, here is just the width of the screen fill_parent


protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int childCount = Getchildcount (); for (int i = 0; i < ChildCount; i++) {View child = Getchildat (i); int row = i% colcount;//number of lines int col = i/colcount;//number of columns    int w1 = Child.getmeasuredwi DTH ();    int padding = ITEMWIDTH-W1;    if (padding >= 5) {    padding = 5;//This is to allow each textview to have a left spacing, you can calculate the width of the text content to be calculated in the middle of the word,    }int left = row * Itemwidt H + padding;int top = col * Child.getmeasuredheight (); int right = left + Itemwidth;int bottom = top + Child.getmeasuredhei Ght (); Child.layout (left, top, right, bottom);}}

Data methods.

public void Addchildviews (arraylist<student> list) {if (list = = null) return;for (Student c:list) {AddView (Createi Temview (C.stuid)); AddView (Createitemview (C.stuname)); AddView (Createitemview (C.stufrom)); AddView (CreateItemView (c.studate)); AddView (Createitemview (C.sturoom)); AddView (Createitemview (C.stuclass));} Courselist = List;int TotalRow = (courselist.size ()/colcount) * COLCOUNT; LOG.D ("", "TotalRow:" + TotalRow);} Private ViewGroup Createitemview (String text) {ViewGroup v = (viewgroup) layoutinflater.from (GetContext ()). Inflate ( R.layout.item_view, NULL);((TextView) V.findviewbyid (R.id.text)). SetText (text); return v;}

Item_view Layout Content

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">    <textview         android:id= "@+id/text"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:text= ""         android:textsize = "16sp"        android:textcolor= "#00CD00"        /></relativelayout >

Okay, now the data basically shows up on the UI, but it's not drawing lines yet. We need a carbon copy. Dispatchdraw method for canvas painting

protected void Dispatchdraw (canvas canvas) {super.dispatchdraw (canvas); LOG.D ("", "width:" + itemwidth + "Heigh:" + itemheight);//Draw horizontal line int totalrow = Courselist.size ();     for (int i = 0; I <= totalrow; i++) {     int starty = i * itemheight;     int stopy = Starty;     Canvas.drawline (0, Starty, itemwidth * colcount, stopy, linepaint);     }     Draw vertical line for     (int i = 0; I <= colcount; i++) {     int startX = i*itemwidth;     int stopx  = i*itemwidth;     int starty = 0;     int stopy = ItemHeight * TotalRow;     Canvas.drawline (StartX, Starty, Stopx, Stopy, linepaint);     }        }

Drawing lines is the process of calculation, with each item's width and height, and the following is the line's property code.

Private Paint linepaint;private void init () {        linepaint = new Paint (paint.anti_alias_flag);        Linepaint.setstyle (Paint.Style.STROKE);        Linepaint.setcolor (Color.gray);        Linepaint.setstrokewidth (0.5f);}

The table does not show the header in fact this can be drawn, or with the layout of a patchwork is also possible.

Come here today, please point out the question, thank you.




Android Custom ViewGroup Implementation table display student information

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.