2011-02-10 10 views
2

私はgridviewを持っていますが、motioneven.action_downのものを実行し、motioneven.action_upのために何かをする必要があります...この必要な機能を私に与えてはいけません。Android onclicklistnerを使用せずにgridviewから選択した項目の位置を取得する方法、ontouchlistnerを代わりに使用する方法

gridviewを簡単に呼び出して、選択した項目をontouchlistenerで取得する方法はありますか?私は自分自身の実装を作ることには限界がありました。それは右のx、yを得るのは難しいので、もし私が子を呼び出すと、xとyは子に相対的なので、ボタンは0,0〜48,48になりますが、画面上の実際の位置は教えてくれませんグリッドビューまたはスクリーン自体を基準にしています。

これは私がやってきたことです、これまでのところ部分的に働いています。

Grid.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        int x = (int)event.getX(); 
        int y = (int)event.getY(); 
        int position = 0; 
        int childCount = Grid.getChildCount(); 

        Message msg = new Message(); 
        Rect ButtonRect = new Rect(); 
        Grid.getChildAt(0).getDrawingRect(ButtonRect); 
        int InitialLeft = ButtonRect.left + 10; 
        ButtonRect.offsetTo(InitialLeft, ButtonRect.top); 
        // 

        while(position < childCount){ 
         if(ButtonRect.contains(x,y)){break;} 
         if(ButtonRect.right + ButtonRect.width() > Grid.getWidth()) 
         { ButtonRect.offsetTo(InitialLeft, ButtonRect.bottom);} 
         position++; 
         ButtonRect.offsetTo(ButtonRect.right, ButtonRect.top); 

        } 

        msg.what = position; 
        msg.arg1 = ButtonRect.bottom; 
        msg.arg2 = y; 
        cHandler.sendMessage(msg); 
       }// end if action up 

       if (event.getAction() == MotionEvent.ACTION_UP) { 
       } 
       return false; 
      } 
     }); 
+0

私はあなたがこの記事で答えを見つけるかもしれないと思う:http://stackoverflow.com/questions/2217670/android-how-to-detect-double-tap(あなたはまだそれを探している場合):) –

答えて

-1

onItemClickListenerを使用して、グリッド表示の特定の項目を取得します。

@Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     // TODO Auto-generated method stub 

     // here arg2 is the position of the grid view item on which you have clicked on 

    } 

お試しください。わたしにはできる。

+0

どこでこのメソッドをオーバーライドしますか? ontouchlistnerの中に?私はこのonitemclickを使ってポジションを得ることができますが、これでkeydown/keyupを得ることはできません。 – zonemikel

+0

なぜあなたはonTouchListenerを使いたいですか?グリッドビューには、onItemClickListenerを直接使用します。 – Debarati

+0

@zonemikelメソッド 'onItemClick()'はOnItemClickListenerインタフェースで定義されています。 'mGrid.setOnItemClickListener(){ @Override public void onItemClick(AdapterView arg0、View arg1、int arg2、long arg3){ //あなたのグリッドで動作させるには、生成されたメソッドスタブ} \t \t}); – Ridcully

関連する問題