Here is the code from my subclass of ListView . It can be easily adapted so that it can be used in the opcode.
getListItemsHeight() returns the total pixel height of the list and fills the array with vertical pixel offsets of each element. Although this information is valid, getListScrollY() returns the current scroll position of the vertical pixel, and scrollListToY() scrolls the list to the position of the pixel. If the size or contents of the list changes, you must call getListItemsHeight() again.
private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } }
dslamnig
source share