1

I have a GridView that implemented onItemclickListener and also onfling .The problem is when i do fling(swipe left to right), it works fine but itemclick event of GridView is always getting wrong position(position always show the very first item of the row of clicked item).How to overCome this problem? Help me Finding please . Thanks for your time ^_^ This is the code for 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);
    }

This is the code for 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);

        }
    });
Chann Lynn
  • 201
  • 1
  • 3
  • 14
  • Probably a duplicate: http://stackoverflow.com/questions/7238855/gridview-with-a-baseadapter-onitemclick-giving-wrong-position – tobidude Jan 23 '17 at 18:09

1 Answers1

0

try this it will give you exact position for example Log.i("clicked", (position- gridview.getFirstVisiblePosition())+"");

Maulik.J
  • 636
  • 1
  • 6
  • 14