Reputation: 201
I have a calendar like gridview with both gesture and OnItemClickListener . When the user swipe from left to right the grid view will show next month . When the user swipe the grid view from right to left , it will show the previous month . I use SimpleOnGestureListener for those events . When I click the item of the gridview , I would like to display the details of the current Item . The problem is When I do only one of those events(Gesture and OnItemClickListener), they work fine . But When I do both , the gesture work fine and OnItemClickListener doesn't get clicked view , it always get the first view of the row of the clicked item . Help me please . Sorry if my question Bother you , Plus Apologies for my poor English . Thanks .
This is the Code I used to detect Gesture :
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() {
}
And this is the code For onItemClickListener
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);
}
});
Upvotes: 5
Views: 1977
Reputation: 1308
i know its old question, but i just encounter same problem recently, so ill try to answer.
I found it hard to use Gesture Class, so instead i use onTouch event, like this :
this.mMonthYearContainer.setOnTouchListener(this);
then implement the event methods
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;
}
in this case i put the listener to another view that show the Month and year, but you maybe can implement same method to the calendarview itself
Upvotes: 0
Reputation: 373
I have same problem. Return false in onDown method helps me:
@Override
public boolean onDown(MotionEvent e) {
return false;
}
Upvotes: 5