0

I am using a customadapter and overriding the getView() method. In this method I call view.getHeight() which returns 0. I would like to find out the height (in pixel) of each item in the listview. And find out where it is on the screen. So If I have 10 items in the list view is it possible to find out where the 5th item position on the screen(in pixel) I would like to find out the y of the item. top left corner.

CustomArrayAdapter adapter = new CustomArrayAdapter(this,R.layout.rowlayout,mydizi);
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    lv.addHeaderView(header);


        lv.setAdapter(adapter);
        View viewList = lv.getChildAt(5 - lv.getFirstVisiblePosition());
        viewList.getLocationOnScreen(location);
akd
  • 6,538
  • 16
  • 70
  • 112

2 Answers2

0

You might need to use something like the ViewTreeObserver. Please see this question's response. You can place a block similar to this in your onCreate method. If a view is returning a value of 0 it is most likely that your are calling the view method too early in the activity lifecycle (ie before the screen objects are mapped out and able to be measured).

Community
  • 1
  • 1
thomas.cloud
  • 881
  • 13
  • 30
0

For a given VISIBLE row nPosition in list mList, you can get the location of the view via:

// find the position of the source and destination accounts ListView rows
int[] location = {0,0};

View viewList = mList.getChildAt(nPosition - mList.getFirstVisiblePosition());
viewList.getLocationOnScreen(location);

but you'll likely need to do this from outside your getView(), i.e. after the list is populated.

CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Thanks for this. But I couldnt get it worked. I just added the code where I put the code but it generates error on the line viewList.getLocationOnScreen(location); nullpointerexception. – akd Jul 26 '12 at 18:35