2012-09-27 114 views
1

在android中,我如何獲得listview的滾動位置?Android獲取滾動位置的listview填充不同的高度

我知道,我可以檢索均勻填充列表視圖的滾動位置用下面的代碼:

int scrollY = -this.getChildAt(0).getTop() + this.getFirstVisiblePosition()* this.getChildAt(0).getHeight(); 

的代碼假定孩子(項目)的所有高度在ListView等於(this.getChildAt(0).getHeight()

現在,如果我填充我的listview與不等大小的項目,我如何得到正確的滾動位置?

我的列表視圖看起來是這樣的: enter image description here

這就是爲什麼我需要滾動位置:

private Canvas drawIndicator(Canvas canvas) { 

    int scrollY = getCurrentScrollPosition(); 



    paint.setColor(Color.GRAY); 
    paint.setAlpha(100); 
    canvas.drawRect(getLeft(), indicatorPosition[0] - scrollY, getRight(), indicatorPosition[1] 
      - scrollY, paint); 

    //Log.d(VIEW_LOG_TAG, "drawIndicator:" + (indicatorPosition[1] - 
    //scrollY)); 

    paint.setColor(Color.parseColor("#47B3EA")); 
    canvas.drawRect(getLeft(), indicatorPosition[1] - scrollY - (indicatorHeight/2), 
      getRight(), indicatorPosition[1] - scrollY + indicatorHeight, paint); 

    return canvas; 
} 

我需要繪製下面的列表視圖的滾動一個指標

我會援引它像

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.save(); 
    canvas = drawIndicator(canvas); 
    super.onDraw(canvas); 
    canvas.restore(); 
} 
+0

你能解釋一下爲什麼你需要滾動位置? –

+0

請檢查的問題,我已經加入然而之所以 – tom91136

回答

3

假設你知道你的所有項目類型的大小:

int currentY = 0; 
     for (int i = 0; i < listView.getFirstVisiblePosition(); i++) { 

      int type = listView.getAdapter().getItemViewType(i); 
      currentY += getHightForViewType(type); 
     } 

     int scrollY = -listView.getChildAt(0).getTop() + currentY; 

和使用適配器:

private int getHightForViewType(int itemViewType) { 

     int hightItem; 
     switch (itemViewType) { 
      case 0: 
       hightItem = 100; 
       break; 

      default: 
       hightItem = 60; 
       break; 
     } 
     return hightItem; 

    } 
+0

這個答案好,如果您想知道爲什麼滾動位置不夠準確,您還需要考慮將divider高度添加到currentY中。 – thehayro

1

或:

private Dictionary<Integer, Integer> listViewItemHeights = new Hashtable<Integer, Integer>(); 

private int getScroll() { 
    View c = listView.getChildAt(0); //this is the first visible row 
    int scrollY = -c.getTop(); 
    listViewItemHeights.put(listView.getFirstVisiblePosition(), c.getHeight()); 
    for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) { 
     if (listViewItemHeights.get(i) != null) // (this is a sanity check) 
      scrollY += listViewItemHeights.get(i); //add all heights of the views that are gone 
    } 
    return scrollY; 
} 

這應該被調用

public void onScroll(AbsHListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 

will w只有在從位置0滾動時才手動完成,而不是以編程方式完成。

0

我面臨同樣的問題,但我用每個項目內的隨機長文本listView,因此我不知道項目高度提前。 我的實現:

private static int[] heights; 

public static int getScrollY(ListView lv) { 
    if (heights == null || lv.getCount() != heights.length) { 
     heights = new int[lv.getCount()]; 
    } 
    View c = lv.getChildAt(0); 
    if (c == null) { 
     return 0; 
    } 

    int firstVisiblePosition = lv.getFirstVisiblePosition(); 
    if (firstVisiblePosition < lv.getCount() && heights[firstVisiblePosition + 1] == 0) { 
     heights[firstVisiblePosition + 1] += heights[firstVisiblePosition] + c.getHeight(); 
    } 
    return -c.getTop() + heights[firstVisiblePosition]; 
} 

我認爲有,如果ListView中的任何元素改變(添加/刪除/改變高度)的問題。當你滾動listView時也會計算高度,如果以編程方式滾動listView則會出現問題。 這是很可悲的,是的ListView implementaion防止方便地訪問滾動Y位置:(