In Android tablet fragment, the two adjacent cells in the same row of tablelayout are merged into one cell for display?

Source: Internet
Author: User

The effect of merging two cells into one cell is shown in:

How to achieve the above results:

1. Get tablelayout.

2. Use tablelayout obtained in the first step to call the following method:

Private void appendtable (tablelayout ){
Int size = filelist. Size ();
Int columnnum = 4;
Boolean isspancell = false;
Boolean startnewrow = true;
Boolean endofonerow = false;
Table. setpadding (10, 0, 5, 0 );

// Obtain the fragment width of tablelayout,
Int width = fragment. getview (). getwidth ();
Tablerow ROW = NULL;
For (Int J = 0; j <size; j ++ ){
// The red code below todo is the rule for merging two adjacent cells I have defined. You can define your own merging rules.
If (J % columnnum = 0 ){
Startnewrow = true;
}
Else {
Startnewrow = false;
}
If (J % columnnum = 3 | j = size-1 ){
Endofonerow = true;
}
Else {
Endofonerow = false;
}
If (J % 5 = 0 | (J + 2) % 20 = 0 ){
Isspancell = true;
}
Else {
Isspancell = false;
}
If (startnewrow ){
Row = new tablerow (this. context );
}
View cell = generatecell (J, isspancell, width );

// Add new cells to the current row

Row. addview (cell );

// If a row ends, tablelayout adds the current row.

If (endofonerow ){
Row. setpadding (0, 0, 0, 10 );
Tablelayout. addview (ROW );
}
}
}

3. The method for green in step 2 is as follows:

Private view generatecell (INT index, Boolean isspanrow, int width ){
Viewholder holder = new viewholder ();
View view = NULL;
View = Inflater. Inflate (R. layout. file_item, null );

Tablerow. layoutparams TLP = new tablerow. layoutparams ();
TLP. span = 2; // tablerow. layoutparams, set cross-two columns
Linearlayout layout = new linearlayout (context );
Holder. filecreatoricon = (imageview) view
. Findviewbyid (R. Id. file_creator_icon );
Holder. filegridviewitemlayout = (relativelayout) view
. Findviewbyid (R. Id. file_gridview_item_relativelayout );
Holder. fileupdatedtime = (textview) view
. Findviewbyid (R. Id. file_upload_time );
Holder. fileownername = (textview) view. findviewbyid (R. Id. file_text_owner );
Holder. filename = (textview) view. findviewbyid (R. Id. file_name );

Layout. setpadding (0, 0, 10, 0 );

// Determine the width of the current cell based on whether it is cross-column, and dynamically calculate it based on the width of the current fragment.
If (isspanrow ){
Layout. addview (view, (width-40)/5*2 + 10,150 );
Layout. setlayoutparams (TLP );
}
Else {
Layout. addview (view, (width-40)/5,150 );
}


// Display the method of an item, which can be implemented according to your own situation
Showfileinfo (holder, index, view );
Return layout;
}

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.