2012-09-30 48 views
2

我的代碼的目標是將ImageView移動到單擊項目的中心,以某種方式「突出顯示」它。爲此,我試圖獲取列表視圖的單擊項的Y座標。 我的代碼是:如何在列表視圖上獲取項目的Y座標

private OnItemClickListener listview_auto_listener = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View view, int position, 
      long arg3) { 

     int[] coords = new int[2]; 

     view.getLocationOnScreen(coords); 

     // We need just the top coordinate 
     int top = coords[1]; 
     // And bottom 

     moveSelectedCar(top); 

    } 
}; 

其中moveSelectedCar是:

private void moveSelectedCar(int new_y) { 

    TranslateAnimation _tAnim = new TranslateAnimation(
      TranslateAnimation.RELATIVE_TO_SELF, 
      TranslateAnimation.RELATIVE_TO_SELF, last_y, new_y); 

    _tAnim.setInterpolator(new DecelerateInterpolator()); 
    _tAnim.setDuration(800); 
    _tAnim.setFillAfter(true); 
    _tAnim.setFillEnabled(true); 

    img_selected_car.startAnimation(_tAnim); 

    last_y = new_y; 

} 

的問題是,這並不返回被點擊項目的中心。

+0

你是什麼意思不返回到被點擊項目的中心?你發佈的動畫代碼是做什麼的? – Givi

+0

動畫代碼必須將ImageView居中到點擊行的中心位置。問題是要找到這個中心....我試圖在上面的代碼上做到這一點。任何幫助? –

回答

2

(。在一個問題回答的編輯轉換爲一個社區維基答案見Question with no answers, but issue solved in the comments (or extended in chat)

的OP寫道:

我已經解決了這個問題!

private OnItemClickListener listview_auto_listener = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View view, int position, 
      long arg3) { 

     int new_top = view.getTop() + view.getRight(); 
     int view_height = view.getHeight(); 

     moveSelectedCar(new_top - view_height); 

    } 
}; 

我希望這可以幫助別人!