开发者

GridView onTouch

I am trying to set up a GridView of buttons that responds to onTouch. For example, if I swipe my finger across the screen horizontally, vertically or diagonally then I want the buttons which were touched to be selected. I tried setting OnTouchListener's for the buttons, but this didn't work, only the first button in the drag event received the onTouch events.

Next I tried creating an OnTouchListener for the GridView and then using the X,Y coords to determine which buttons where touched. This doesn't seem to work either since th开发者_Go百科e MOTION_DOWN events are only passed to the GridView's onTouch() if I start the drag on the very edge of the GridView. In fact if I drag my finger horizontally across the grid, the onTouch events aren't fired at all unless I start the drag at the edge of the GridView. Does anyone know of a better way 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;
 }
}


If you have an adapter implement an interface to listen click events on your gridcell, for instance:

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

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

And you should implement in your adapter getView(..) method, the 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){}

            }
        });

It's recommended to use a holder to your view elements inside the adapter.

And the final part, from your Activity class, just call:

    adapter.setOnSaveEditsListener(new GridCellAdapter.OnSaveEditsListener() {

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


You might want to try use the GestureDetector to get the correct touch events fired. From there on, you should be able to determine what you did and where exactly.

GestureDetector will give you several methods telling you what you did (SingleTapUp, Fling, Scroll, ...) and at what position and how long you did this (if you for example swipe, you get the starting position, the duration of the swipe and how far you swiped)


It is not complete but can help 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;
        }
    });


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜