Dynamically set the GridView height, fixed column, for highly adaptive

Source: Internet
Author: User

Dynamically sets the height of the GridView, fixed column, and sets the height according to the item number in the GridView:

Call the following methods:


 public static void Setlistviewheightbasedonchildren (GridView listview) {//Get ListView Adapter ListAdapter List        Adapter = Listview.getadapter ();        if (ListAdapter = = null) {return;        }//fixed column width, how many columns int col = 4;//Listview.getnumcolumns ();        int totalheight = 0; I add 4 each time, equivalent to Listadapter.getcount () is less than or equal to 4 o'clock cycles once, calculates the height of the item once,//Listadapter.getcount () is less than or equal to 8 o'clock calculates two times the height of the add for (int i = 0; I < Listadapter.getcount ();            i + = col) {//Get ListView for each item View ListItem = Listadapter.getview (i, NULL, ListView);            Listitem.measure (0, 0);        Gets the height of item and Totalheight + = Listitem.getmeasuredheight ();        }//Get the layout parameters of the ListView viewgroup.layoutparams params = Listview.getlayoutparams ();        Set height params.height = totalheight;        Set the margin ((marginlayoutparams) params. SetMargins (10, 10, 10, 10);    Set the parameter listview.setlayoutparams (params);} 
After calling this method, you need to invoke the Notifydatasetchanged () method to implement the interface refresh

Dynamically set the GridView height, fixed column, for highly adaptive

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.