2013-03-13 2 views
1

상단에서 특정 오프셋에있는 목록 항목의 색인을 가져 오는 간단한 방법이 있습니까? 예를 들어 상단에서 150 픽셀 떨어진 항목의 색인을 가져옵니다.안드로이드에서 특정 오프셋에서 항목의 색인을 얻는 방법 ListView

+0

어쩌면 당신은 당신이하고자하는 것에 대해 더 설명 할 수 있습니다 - 이것은 잘 풀리지 않는 접근 방식일지도 모릅니다. – NPike

+0

화면 가운데에있는 항목의 색인이 무엇인지 알고 싶습니다. –

답변

1

당신의 목표는 화면의 중앙에있는 목록 항목을 찾을 수 있기 때문에, 당신은 뭔가를 시도 할 수 있습니다 다음은 :

(예 : MyListView.java 등의 ListView를 확장하는 사용자 정의 클래스를 확인)

public class MyListView extends ListView implements OnScrollListener { 

public MyListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setOnScrollListener(this); 
    } 


@Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Center the child currently closest to the center of the ListView when 
     // the ListView stops scrolling. 
     if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { 

      int listViewCenterX = getWidth()/2; 
      int listViewCenterY = getHeight()/2; 

      Rect rect = new Rect(); 

      // Iterate the children and find which one is currently the most 
      // centered. 
      for (int i = 0; i < getChildCount(); ++i) { 
       View child = getChildAt(i); 
       child.getHitRect(rect); 
       if (rect.contains(listViewCenterX, listViewCenterY)) { 
        // this listitem is in the "center" of the listview 
        // do what you want with it. 
        final int position = getPositionForView(child); 
        final int offset = listViewCenterY - (child.getHeight()/2); 
        break; 
       } 
      } 
     } 
    } 


    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

    } 
} 
0

당신은 아이들을 반복하고 각 사람은 당신이 가지고있는 점에 대한 사각형을 칠 확인할 수 있습니다.

관련 문제