android - List view height return zero only for first item -


i trying list view heigh @ run time fit item height of screen.

here code: in getview()

int totalheight = activity.getlistview().getheight();             int rowheight = totalheight / getcount(); // divide number of items.             activity.getlistview().setscrollcontainer(false);             activity.getlistview().setverticalscrollbarenabled(false);             convertview.setminimumheight(rowheight); 

in activity have programatically created list view both width , height match_parent.

but tried debug , saw first item list view height 0 rest of items giving me proper height.

why ? how fix ?

it's due view has not been created yet. in situation height 0.

you can height of view using like:

final listview listview = activity.getlistview(); viewtreeobserver vto = listview.getviewtreeobserver(); vto.addongloballayoutlistener(new ongloballayoutlistener() {      @override     public void ongloballayout() {         int totalheight = listview.getheight();         int rowheight = totalheight / getcount(); // divide number of items.         listview.setscrollcontainer(false);         listview.setverticalscrollbarenabled(false);         convertview.setminimumheight(rowheight);          viewtreeobserver obs = listview.getviewtreeobserver();             if (build.version.sdk_int >= build.version_codes.jelly_bean) {             obs.removeongloballayoutlistener(this);         } else {             obs.removeglobalonlayoutlistener(this);         }     }  }); 

the reason checking build.version in sdk prior jelly bean name of method different (ongloballayout vs globalonlayout)


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -