如何在listview上获取项目的Y坐标

时间:2012-09-30 19:55:50

标签: android listview coordinate

我的代码的目标是将ImageView移动到点击项目的中心,以某种方式“突出显示”它。为此,我正在尝试获取listview的单击项的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;

}

问题是这不会返回所点击项目的中心。

1 个答案:

答案 0 :(得分: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);

    }
};
  

我希望这有助于某人!