网格视图给出错误的位置

时间:2014-02-03 09:22:21

标签: android gridview

我有一个GridView,它实现了onItemclickListener并且还在onfling。问题是当我执行fling(从左向右滑动)时,它工作正常但是GridView的itemclick事件总是出错位置(位置总是显示第一个项目点击项目的行)。如何解决此问题?请帮我找。谢谢你的时间^ _ ^ 这是onFling的代码:

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }

这是ItemClickListener的代码

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
    int position, long id) {
            Log.i("clicked", position+"");

            ((CalendarAdapter) parent.getAdapter()).setSelected(v);
            String selectedGridDate = CalendarAdapter.dayString
                    .get(position);
            String[] separatedTime = selectedGridDate.split("-");
            String gridvalueString = separatedTime[2].replaceFirst("^0*",
                    "");
            int gridvalue = Integer.parseInt(gridvalueString);
            // navigate to next or previous month on clicking offdays.
            if ((gridvalue > 10) && (position < 8)) {
                setPreviousMonth();
                refreshCalendar();
            } else if ((gridvalue < 7) && (position > 28)) {
                setNextMonth();
                refreshCalendar();
            }

((CalendarAdapter) parent.getAdapter()).setSelected(v);

        }
    });

1 个答案:

答案 0 :(得分:0)

尝试这个它会给你准确的位置,例如Log.i(“clicked”,(position- gridview.getFirstVisiblePosition())+“”);