You need to listen and touch the GridView and find out the position number.

I recognize the number as follows:

GridView gv_month; gv_month.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //parent.getItemAtPosition(position).toString(); Log.d("MyLog","day : " + String.valueOf(position) }); 

Touch as follows:

 gv_month.setOnTouchListener(new AdapterView.OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event){ switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: switch (v.getId()) { case R.id.gv_month: fromPosition = event.getX(); default: break; } return true; case MotionEvent.ACTION_UP: switch (v.getId()) { case R.id.gv_month: float toPosition = event.getX(); if (fromPosition > toPosition){ Log.d("MyLog","LEFT"); } else if (fromPosition < toPosition){ Log.d("MyLog","RIGHT"); } default: break; } } return true; } return false; } 

If I implement both listeners, then only the second one works. How to combine?

  • You only use Log.d("MyLog","day : " + String.valueOf(position) so why not put it after the line, for example, fromPosition = event.getX(); and transfer the position to X . And how do you define X , also define Y - Dred
  • GridView each time has a different number of cells and sometimes there are three cells in a row on the screen, sometimes up to 6. In general, it will be difficult to calculate which cell was pressed using X and Y. Or I misunderstood the idea with the coordinates. - kaaa
  • Right. Only what coordinates do you need? relative to the entire screen or cell? In fact, it is possible to calculate relative to the entire screen. - Dred
  • I need to know the cell number that was touched. Calculate it using the coordinates is difficult, because the number of cells is always different and they are arranged differently each time. At the same time, I need to track, it was just a touch, or it was moved across the screen at the location of the GridView. - kaaa
  • Maybe this will help you - Dred

1 answer 1

return true should be replaced with return false, then touch processing is not interrupted.

  • and the casket just opened :) Thank you! - kaaa