Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid View with gesture and on item click listener

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);

        }
    });
like image 457
Chann Lynn Avatar asked Feb 02 '14 05:02

Chann Lynn


1 Answers

I have same problem. Return false in onDown method helps me:

@Override
public boolean onDown(MotionEvent e) {
    return false;
}
like image 194
Rustam Iuzmukhametov Avatar answered Oct 02 '22 12:10

Rustam Iuzmukhametov