Gridview ontouch

I am trying to customize a gridview of buttons that respond to onTouch. For example, if I swipe the screen horizontally, vertically or diagonally, I want the buttons that have been touched to be selected. I tried setting OnTouchListener for the buttons, but that did not work, only the first button in the drag event received onTouch events.

Next, I tried to create an OnTouchListener for the GridView, and then using the X, Y codes to determine which buttons are touching. This does not seem to work, since the MOTION_DOWN events are only dispatched to the GridView onTouch () if I start dragging along the very edge of the GridView. In fact, if I drag my finger horizontally across the grid, the onTouch events do not fire at all, unless I started dragging along the edge of the GridView. Does anyone know how best to do this?

Thanks in advance.

gridview.setOnTouchListener(new View.OnTouchListener()
{
 @Override
 public boolean onTouch(@SuppressWarnings("unused") View view, MotionEvent event)
 {
  float X = event.getX();
  float Y = event.getY();

  switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN:
   System.out.println("Down: " + X + "," + Y);
   break;
  case MotionEvent.ACTION_MOVE:
   System.out.println("Move: " + X + "," + Y);
   break;
  case MotionEvent.ACTION_UP: 
   System.out.println("Up: " + X + "," + Y);
   break; 
  }

  return true;
 }
}
+3
source share
4 answers

If you have an adapter that implements an interface to listen for click events on your grid cell, for example:

public interface OnSaveEditsListener
{
    public abstract void onSaveEdits();
}

private OnSaveEditsListener saveEditsListener = null;
public void setOnSaveEditsListener(OnSaveEditsListener l) {
    saveEditsListener = l;
}

And you must implement the getView (..) adapter method, a special click listener:

        holder.gridcell.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CalendarPosition = position;
                v.requestFocusFromTouch();

                try{
                    saveEditsListener.onSaveEdits();
                    holder.gridcell.invalidate();
                }catch(Exception ex){}

            }
        });

.

, Activity, :

    adapter.setOnSaveEditsListener(new GridCellAdapter.OnSaveEditsListener() {

        @Override
        public void onSaveEdits() {
            /** methods  *//
    });
}
+1

GestureDetector . , .

GestureDetector , , (SingleTapUp, Fling, Scroll,...) ( , , , , )

0

, u

gridview.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_MOVE){
                gridview.requestFocusFromTouch();
                gridview.setSelection(gridview.pointToPosition((int)event.getX(),(int)event.getY()));
                return true;
            }
            if(event.getAction() == MotionEvent.ACTION_UP){
                gridview.clearFocus();
                return true;
            }

            return false;
        }
    });
0

You are on the right track. You just need to use the X, Y coordinates from onTouch to get the grid cell position using the gridView method pointToPosition. Then you can use the position (ID) to get the actual element in the cell using the method getChildAt. For example, if your gridView has a TextView in each cell, use this

int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
TextView tv = (TextView) gridView.getChildAt(position);
0
source

Source: https://habr.com/ru/post/1745285/


All Articles