2012-07-26 82 views
0

我正在使用customadapter并重写getView()方法。在这个方法中,我调用view.getHeight()返回0.我想找出列表视图中每个项目的高度(以像素为单位)。并找出它在屏幕上的位置。因此,如果我在列表视图中有10个项目,可以找出屏幕上第5个项目的位置(以像素为单位),我想找出项目的y。左上角。如何获得listview中项目的高度?

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); 

回答

0

您可能需要使用诸如ViewTreeObserver之类的东西。请参阅this问题的回应。您可以在onCreate方法中放置一个与此类似的块。如果视图返回值为0,则很可能是在活动生命周期中过早调用视图方法(即在屏幕对象被映射出来并能够被测量之前)。后

// 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); 

,但你可能需要从getView(外做到这一点),即:

0

对于列表mList给定可见行nPosition,您可以通过获取视图的位置列表已填充。

+0

感谢您的支持。但我无法得到它的工作。我只是在代码的位置添加了代码,但它在行viewList.getLocationOnScreen(location)上产生了错误;空指针异常。 – akd 2012-07-26 18:35:17