2013-01-15 62 views
0

我跟隨this tutorial,除了我試圖給每個圖像添加一個「觸摸」外觀。當你也有onTouchListener時,你如何觸發onClickTextListener?

因此,大家可以看到,HelloGridView是完全一樣的教程:

public class HelloGridView extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 

     gridview.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

對於ImageAdapter,我添加了一個onTouchListener

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    // create a new ImageView for each item referenced by the Adapter 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(300, 300)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 

     imageView.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_DOWN) { 
        Log.v("log_tag", "MotionEvent.ACTION_DOWN"); 
        return true; 
       } else if(event.getAction()==MotionEvent.ACTION_UP){ 
        Toast.makeText(mContext, "ACTION_UP", Toast.LENGTH_SHORT).show(); 
        Log.v("log_tag", "MotionEvent.ACTION_UP"); 
        return false; 
       } else { 
        return false; 
       } 
      } 
     }); 

     return imageView; 
    } 
} 

的問題是,每當我接觸的一個圖像onTouchListenerImageAdapter中發生火災,但在HelloGridView中發生了火災onItemClickListener

我想onItemClickListener火上ACTION_UP,所以我返回false,如果該事件是如果MotionEvent.ACTION_DOWNMotionEvent.ACTION_UP和真實的。

從我的理解中,如果我返回true,那麼onTouchListener會消耗事件並結束。但是如果我返回false,那麼onTouchListener不會消耗事件,並​​且它將傳遞到onItemClickListenerHelloGridView

不幸的是,如果我在事件爲ACTION_DOWN時返回false,則該事件僅傳遞到HelloGridView。但是,當我這樣做時,ACTION_UP永遠不會被執行。有人能告訴我我做錯了什麼,我該如何解決它?如果我的問題不清楚,請讓我知道,我會盡力澄清! :)

回答

0

OnItemClickListener只能在Action.Down上觸發。

只需執行以下操作: 擺脫OnItemClickListener並將其替換爲您自己的方法。就像「public void myOnItemActionUp(View view, int pos)」 - 不管。

將您的HelloGridView的引用傳遞給ImageAdapter,並在您的OnTouchListener if (event.getAction() == MotionEvent.ACTION_UP)中調用mHelloGridView.myOnItemActionUp(v, position)

1

代碼,這是

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

     switch(event.getAction()) 
     { 
     case MotionEvent.ACTION_DOWN: 

      break;   
     case MotionEvent.ACTION_MOVE: 

      break; 

     case MotionEvent.ACTION_UP: 

      if(!mIsTouchHandeled){ 
       int position = mChatListView.pointToPosition((int) event.getX(), (int) event.getY()); 
       if(position!=ListView.INVALID_POSITION){ 
        mChatListView.performItemClick(mChatListView.getChildAt(position-mChatListView.getFirstVisiblePosition()), position, mChatListView.getItemIdAtPosition(position)); 
       } 
      } 
      break; 
     }   
     //  return super.onTouchEvent(event);  


     return false; 
    } 

} 
相關問題