我的代码的目标是将ImageView移到单击项的中心,以某种方式“突出显示”它。为此,我试图获取列表视图中单击项的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;
}
问题在于这不会返回所单击项目的中心。
最佳答案
(在问题编辑中作答。转换为社区Wiki答案。请参见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);
}
};
希望对您有所帮助!