2012-07-02 39 views
0

我很难找出这个问题。我有一个8个按钮的网格视图。目前,我正在使用onItemClickListener来触发按钮操作,但是这给我带来了两个问题。同时GridView按钮事件/确定哪个按钮已被触摸

1)的按钮动作发生按钮已被未压制之后。同时

2)两个按钮不能在按下时,你必须释放第一个按钮。

正如我已经学会了,一个onTouchListener应该可以解决我的第一个问题,虽然我不知道如何确定已按下哪个按钮。我的onItemClickListener代码如下

gridview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

     Toast.makeText(Activity.this, "" + position, Toast.LENGTH_SHORT).show(); 

    } 
     }); 

现在,我已经知道哪个按钮被按下了。我相信,实现代码为onTouchListener如下

 gridview.setOnTouchListener(new OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 
      return false; 
     } 
    }) { 

我怎么确定哪些按钮被使用MotionEvent何苦呢?在我通过“职位”之前,这使得这很容易。我还需要考虑是否有两个或更多的按钮被同时按下/不让另一个按下。

有谁知道如何做到这一点?

回答

0

我INFACT试图找出同样的事情了。我得到尽可能找出使用下面的代码

public boolean onTouch(View v, MotionEvent me) { 

      float currentXPosition = me.getX(); 
      float currentYPosition = me.getY(); 
      int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition); 

的位置让你在GridView上的数量已被点击其中栅格单元,并且可以推测检索特定项目如下

gridView.getItemAtPosition(position) 

但那是我卡住的地方。我的gridView中有Textview项目,我无法将项目转换为文本视图,然后对其执行操作。

希望这会有所帮助!

0

当使用GridView中的理念是:

  • 网格视图实现onTouchLister
  • 当发生触摸onTouchLister采集坐标(很多:)) 所有ACTION_MOVE事件
  • 当触摸事件是MOVE_UP,根据 计算坐标下的实际位置并返回网格中的项目

S Ø解决办法是:

在你的活动,你必须findViewById(some_grid_view)

//Register handler for the onTouch event of gridView in your activity 
gridView.setOnTouchListener(new MyActivityOnTouchListener(this)); 

:不是活动

在我的onTouch听众在另一个类(MyActivityOnTouchListener)实施

...那么在MyActivityOnTouchListener类需要实现onTouch方法:

public class CalendarActivityOnTouchListener implements View.OnTouchListener { 
    private MyActivity myActivityContext; 
    private GridView mGridView; 
    private HashSet<Point> movementCoordinates = new HashSet<Point>; 

    //Constructor 
    public MyActivityOnTouchListener (MyActivity context){ 
     this.myActivityContext= context; 
     mGridView= myActivityContext.getGridView(); //assign touched gridView into a local variable 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

    /* 
    * NOTE: 
    * ACTION_MOVE fires events until you release it 
    * ACTION_UP once you release it fires it 
    */ 

    //while touching the grid a bunch of ACTION_MOVE events are dispatched 
    if (event.getAction() == MotionEvent.ACTION_MOVE) { 
     //gather all coordinates touched (in a set to avoid duplicates) 
     movementCoordinates.add(new Point((int)event.getX(), (int)event.getY())); 
     return true; 
    } 

    //Finally the finger is lifted 
    if (event.getAction() == MotionEvent.ACTION_UP) { 

     //convert all movementCoordinates gathered in the previous block into real grid positions 
     int position; 
     for(Point p : movementCoordinates){ 
      Log.d("Luka", p.x +"/"+p.y); 
      position = calendarGridView.pointToPosition(p.x, p.y); 

      //...Do whatever with the position 
     } 
    } 
    } 

}

要小心的pointToPosition()方法因为在某些情况下,它可以返回-1,而不是后面的位置坐标。举例来说,如果你有项目之间的利润率在网格这些坐标不能返回的位置,因此-1

希望它可以帮助...

1

已经创出近期就此问题与跨越这个职位未来我寻求帮助,我想从我所做的似乎已经工作的东西中添加两件事:

1)我将onTouchListener添加到适配器中的对象而不是活动或gridview。 2)在OnTouchListener中,我查找了MotionEvent.ACTION_DOWN(第一手指触摸)和MotionEvent.ACTION_POINTER_DOWN(后续手指触摸),这样我就可以立即获得多点触摸并立即处理它们,而无需等待用户抬起手指(S)。

注意,我把它叫做ImageAdapter,即使我已经添加了一个TextView每个作为这样我可以使用的图像TextView的背景,但看不见的文本添加到TextView的,因此与对讲工程):

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return numCols * numRows; 
    } 

    public Object getItem(int position) { 
     return this; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    // create a new TextView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView textView; 

     if (convertView == null) { // if it's not recycled, initialize some attributes 
      textView = new TextView(mContext); 

     } else { 
      textView = (TextView) convertView; 
     } 

     // place any other initial setup needed for the TextView here 

     // here's our onTouchListener 
     textView.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       boolean returnValue; 
       int thePosition = v.getId(); 

       // MotionEvent.ACTION_DOWN gets the first touch 
       // MotionEvent.ACTION_POINTER_DOWN gets any subsequent touches (if you place a second finger on the screen) 
       // Between these I can get touches as soon as they happen, including multitouch support, without needing to wait until the user lifts their finger. 

       if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) { 
        TextView textView; 
        if (v == null) { // if it's not recycled, initialize some attributes 
         textView = new TextView(mContext); 

        } else { 
         textView = (TextView) v; 
        } 

        // Do any processing based on the touch - I call a function and pass the position (number of cell, 1..n) and textview so can make changes to it as needed 
        ScreenTapped(thePosition, textView); 
        // I used a returnValue 
        returnValue = true; 
       } else returnValue = false; 

       return returnValue; 

     }); 
     return textView; 

    } // getView 
} //imageadapter