listview - Android #View.getHeight() doesn't return correct value -


i have listview in screen. listview fills 2/3 of screen (so height varies on different devices). have - lets 50 items in array list - need put x number of items in visible user (e.g on small screen 3, on normal screen 5, on tablet 8).

so way i'm doing getting height of listview in px, dp size , since know height of each row 60dp divide height of listviwe in dp format height of row (which 60dp). returns number of items visible user , pass number getcount() method of adapter populate amount of items.

this code, have observer assigned in oncreate() method this.mlistview.getviewtreeobserver().addongloballayoutlistener(mlistviewgloballistener);

        viewtreeobserver.ongloballayoutlistener mlistviewgloballistener = new viewtreeobserver.ongloballayoutlistener()         {             @override             public void ongloballayout()             {                 removelistviewlistener();                  if (mlistview == null || madapter == null)                 {                     return;                 }                  int heightinpx = mlistview.getheight();                 int heightindp = pixelstodp(heightinpx, getresources().getdisplaymetrics());                 int maxdisplaydriver = heightindp / 60;                 madapter.setmaxdisplaydrivers(maxdisplaydriver);                 logger.error("test", "heightinpx:" + heightinpx + ", heightindp:" + heightindp + ", maxdisplaydriver:" + maxdisplaydriver);             }         };      public static int pixelstodp(int px, displaymetrics metrics)     {           return (int) (px / metrics.density);     } 

now when run app have following log: e/test: heightinpx:96, heightindp:32, maxdisplaydriver:0

i believe height wrong because can put @ least 4 items in screen 240dp, while log shows else. idea appreciated. thanks.

you can height of listview way because listview can have n number of childs , getheight() returns actual height not height of visible portion. listview , other similar view doesn't return height system doesn't know because childitems not yet rendered.

so suggested other geeks well, need calculate height of item + margin+padding (if any) , multiply number of child(how know this? read ahead). number of child different in different screen size. need put counter inside getview check how many times getting called oncreate know number of childitem


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -