网格视图与手势和项目点击监听器

时间:2014-02-02 05:03:23

标签: android gridview calendar gesture onitemclicklistener

我有一个类似gridview的日历,包含手势和OnItemClickListener。当用户从左向右滑动时,网格视图将在下个月显示。当用户从右向左滑动网格视图时,它将显示上个月。我将SimpleOnGestureListener用于这些事件。当我单击gridview的项目时,我想显示当前项目的详细信息。问题是当我只做其中一个事件(Gesture和OnItemClickListener)时,它们工作正常。但是当我同时执行这两项操作时,手势工作正常并且OnItemClickListener没有获得单击视图,它始终获得所单击项目行的第一个视图。请帮帮我 。对不起,如果我的问题困扰你,请为我糟糕的英语道歉。谢谢。

这是我用来检测手势的代码:

     public class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 200;
  /*private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;*/

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        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 result;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        Log.i("single", "singletaps");
        return super.onSingleTapUp(e);
    }


}

public void onSingleTap(View v){

}
public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}

这是forItemClickListener的代码

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            ((CalendarAdapter) parent.getAdapter()).setSelected(v);
            String selectedGridDate = CalendarAdapter.dayString
                    .get(position);
            String[] separatedTime = selectedGridDate.split("-");
            String gridvalueString = separatedTime[2].replaceFirst("^0*",
                    "");// taking last part of date. ie; 2 from 2012-12-02.
            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);

        }
    });

2 个答案:

答案 0 :(得分:5)

我有同样的问题。在 onDown 方法中返回false可以帮助我:

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

答案 1 :(得分:0)

我知道它的老问题,但我最近遇到了同样的问题,所以生病了,试着回答。

我发现很难使用Gesture Class,所以我使用onTouch事件,如下所示:

this.mMonthYearContainer.setOnTouchListener(this);

然后实现事件方法

public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()){
            case R.id.activity_calendar_month_container: {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN: {
                        this.mMonthYearContainer_GestureX1 = event.getX();
                        this.mMonthYearContainer_GestureY1 = event.getY();
                    } break;
                    case MotionEvent.ACTION_UP: {
                        this.mMonthYearContainer_GestureX2 = event.getX();
                        this.mMonthYearContainer_GestureY2 = event.getY();
                        float distanceX = this.mMonthYearContainer_GestureX2 - this.mMonthYearContainer_GestureX1;
                        float distanceY = this.mMonthYearContainer_GestureY2 - this.mMonthYearContainer_GestureY1;
                        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > 50) {
                            if (distanceX > 0) {
                                if (this.mSelectedMonth > 0) {
                                    this.mSelectedMonth -= 1;
                                } else {
                                    this.mSelectedMonth = 12;
                                    this.mSelectedYear -= 1;
                                    this.selectMonthAndYear(this.mSelectedMonth, this.mSelectedYear);
                                }
                                this.selectMonthAndYear(this.mSelectedMonth, this.mSelectedYear);
                            } else {
                                if (this.mSelectedMonth < 12) {
                                    this.mSelectedMonth += 1;
                                } else {
                                    this.mSelectedMonth = 1;
                                    this.mSelectedYear += 1;
                                }
                                this.selectMonthAndYear(this.mSelectedMonth, this.mSelectedYear);
                            }
                            return true;
                        }
                        this.mMonthYearContainer_GestureX1 = 0;
                        this.mMonthYearContainer_GestureY1 = 0;
                        this.mMonthYearContainer_GestureX2 = 0;
                        this.mMonthYearContainer_GestureY2 = 0;
                    } break;
                }
            } break;
        }
        return true;
    }

在这种情况下,我将监听器放到显示月份和年份的另一个视图中,但是您可以对calendarview本身实现相同的方法