2012-07-26 37 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

相關問題