Android 15th: ListView adaptive implementation table

Source: Internet
Author: User

The last time we introduced how to use the GridView to implement tables, this 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.

First paste the program running in this article:

The ListView table implemented in this article can have different cell sizes, text (TextView) or image (ImageView) as cell data, there is 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 plaincopy to clipboardprint?
<? 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">
<HorizontalScrollView android: id = "@ + id/HorizontalScrollView01"
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>
</HorizontalScrollView>
</LinearLayout>
<? 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">
<HorizontalScrollView android: id = "@ + id/HorizontalScrollView01"
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>
</HorizontalScrollView>
</LinearLayout>
 

The source code of the main class testMyListView. java is as follows:

View plaincopy to clipboardprint?
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 testMyListView extends 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 in each row
Int width = this. getWindowManager (). getdefadisplay display (). getWidth ()/titles. length;
// Define the 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 row
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,
& Nb

Related Article

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.