Listview adaptive table

Source: Internet
Author: User

This article comes fromHttp://blog.csdn.net/hellogv/, The reference must indicate the source!

Last introducedUse gridview to implement tablesThis time we will talk about how to use listview to implement adaptive tables. Gridview is easier to implement adaptive tables than listview, but the size of each grid unit in the gridview is fixed, while the table in listview can customize the size of each grid unit, however, implementing an adaptive table is also more complex (the size of cells varies ). In addition, the table implemented by the gridview can be located in a specific cell, while the table implemented by the listview can only be located in the table row.So the old saying goes: select the gridview or listview to implement the table based on the specific use environment.

Post this article firstProgramRunning:

The listview table implemented in this article can have different cell sizes, text (textview) or image (imageview) as cell data,No need to pre-define XML implementation styles (Adaptive fundamental goal). Because listview is placed in horizontalscrollview, the width of a data table with multiple or longer columns can be well adapted.

The source code of Main. XML is as follows:

View code

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"Android: Orientation= "Vertical" Android: layout_width = "fill_parent"Android: layout_height= "Fill_parent"> Android: layout_height= "Fill_parent" Android: layout_width = "fill_parent"> <listview Android: Id = "@ + ID/listview01" Android: layout_height = "wrap_content"Android: layout_width= "Wrap_content"> </listview>  

The source code of the main class testmylistview. Java is as follows:

View code

Package  Com. testmylistview;  Import  Java. util. arraylist;  Import  Com. testmylistview. tableadapter. tablecell;  Import  Com. testmylistview. tableadapter. tablerow;  Import  Android. App. activity;  Import  Android. OS. Bundle;  Import  Android. View. view; Import  Android. widget. adapterview;  Import  Android. widget. listview;  Import  Android. widget. linearlayout. layoutparams;  Import  Android. widget. Toast;  /**  *  @ Author  Hellogv  */    Public   Class TestmylistviewExtends  Activity {  /**  Called when the activity is first created.  */  Listview lv; @ override  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); setcontentview (R. layout. Main );  This . Settitle ("listview adaptive table --- hellogv" ); LV = (Listview) This  . Findviewbyid (R. Id. listview01); arraylist <Tablerow> table = New Arraylist <tablerow> (); Tablecell [] titles = New Tablecell [5]; //  Five units per line          Int Width = This . Getwindowmanager (). getdefaultdisplay (). getwidth ()/ Titles. length;  //  Define title         For ( Int I = 0; I <titles. length; I ++ ) {Titles [I] = New Tablecell ("title" + String. valueof (I), width + 8 * I, layoutparams. fill_parent, tablecell. String);} table. Add (  New  Tablerow (titles ));  //  Data in each row Tablecell [] cells =New Tablecell [5]; //  Five units per line          For ( Int I = 0; I <cells. Length-1; I ++ ) {Cells [I] = New Tablecell ("no." + String. valueof (I), titles [I]. Width, layoutparams. fill_parent, tablecell. String);} cells [cells. Length -1] = New Tablecell (R. drawable. Icon, titles [cells. Length -1 ]. Width, layoutparams. wrap_content, tablecell. Image );  //  Add table rows to table          For ( Int I = 0; I <12; I ++ ) Table. Add (  New  Tablerow (cells); tableadapter = New Tableadapter (This  , Table); LV. setadapter (tableadapter); LV. setonitemclicklistener (  New  Itemclickevent ());}  Class Itemclickevent Implements  Adapterview. onitemclicklistener {@ override  Public   Void Onitemclick (adapterview <?> Arg0, view arg1, Int  Arg2,  Long Arg3) {toast. maketext (testmylistview.  This , "Select the" + String. valueof (arg2) + "row", 500 ). Show ();}}} 

Tableadapter. JavaCodeAs follows:

PS: tablecell is a cell class, tablerow is a table row class, And tablerowview is a component that implements table rows.Implementation steps: tablecell --> tablerow (tablerowview) --> listview

View code

 Package  Com. testmylistview;  Import  Java. util. List;  Import Android. content. context;  Import  Android. Graphics. color;  Import  Android. View. gravity;  Import  Android. View. view;  Import  Android. View. viewgroup;  Import  Android. widget. baseadapter;  Import  Android. widget. imageview;  Import Android. widget. linearlayout;  Import  Android. widget. textview;  Public   Class Tableadapter Extends  Baseadapter {  Private  Context context;  Private List <tablerow> Table;  Public Tableadapter (context, list <tablerow> Table ){ This . Context = Context;  This . Table = Table;} @ override  Public   Int  Getcount (){  Return  Table. Size () ;}@ override  Public   Long Getitemid ( Int  Position ){  Return Position ;}  Public Tablerow getitem ( Int  Position ){  Return  Table. Get (position );}  Public View getview ( Int  Position, view convertview, viewgroup parent) {tablerow = Table. Get (position );  Return   New Tablerowview ( This . Context, tablerow );}  /**  * Tablerowview implements table row styles *  @ Author  Hellogv  */        Class Tablerowview Extends  Linearlayout {  Public  Tablerowview (context, tablerow ){  Super  (Context );  This . Setorientation (linearlayout. Horizontal );  For ( Int I = 0; I <tablerow. getsize (); I ++ ){ //  Add cells to rows one by one Tablecell = Tablerow. getcellvalue (I); linearlayout. layoutparams = New  Linearlayout. layoutparams (tablecell. Width, tablecell. Height );  //  Set space according to the size specified by cells Layoutparams. setmargins (0, 0, 1, 1 );//  Reserved gap manufacturing border                  If (Tablecell. type = tablecell. String ){ //  If the cell is text content Textview textcell = New  Textview (context); textcell. setlines ( 1 ); Textcell. setgravity (gravity. center); textcell. setbackgroundcolor (color. Black );  //  Black Background Textcell. settext (string. valueof (tablecell. Value); addview (textcell, layoutparams );}  Else   If (Tablecell. type = tablecell. Image ){ //  If the cell is the image content Imageview imgcell = New  Imageview (context); imgcell. setbackgroundcolor (color. Black );  //  Black Background Imgcell. setimageresource (integer) tablecell. Value); addview (imgcell, layoutparams );}}  This . Setbackgroundcolor (color. White ); //  The background is white and borders are implemented using gaps  }}  /**  * Tablerow implements table rows *  @ Author  Hellogv  */        Static   Public   Class Tablerow {  Private  Tablecell [] cell;  Public  Tablerow (tablecell [] Cell ){  This . Cell = Cell ;}  Public   Int  Getsize (){  Return  Cell. length ;}  Public Tablecell getcellvalue ( Int Index ){  If (Index> = Cell. length)  Return   Null  ;  Return  Cell [Index] ;}}  /**  * Tablecell implements table cells *  @ Author  Hellogv  */        Static   Public  Class  Tablecell {  Static   Public   Final   Int String = 0 ;  Static   Public   Final   Int Image = 1 ;  Public  Object value;  Public   Int Width;  Public   Int  Height;  Private   Int  Type;  Public Tablecell (object value, Int Width, Int Height, Int  Type ){  This . Value = Value; This . Width = Width;  This . Height = Height;  This . Type = Type ;}}} 

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.