2014-12-05 78 views
0

我希望能夠點擊並滾動圖片視圖,但是每當我觸摸圖片視圖並執行「動作移動」時,它都不會滾動,而是執行「點擊事件」。 我希望能夠做到這一點,當我觸摸並移動它應該滾動,但是當我只是觸摸,然後它應該執行點擊! 在此先感謝imageView可點擊和滾動

這是我做了什麼

ImageView ivUser = (ImageView) findViewById(R.id.ivUser); 
    ivUser.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(StoryDetails.this, ProfileView.class); 
      intent.putExtra("id", usersid); 
      Log.d("ivUser attempt", usersid); 
      startActivity(intent); 
     } 
    }); 

我剛剛實施的「onTouch監聽器」類似下面的代碼,但我沒有關於下一步該怎麼做任何想法!

ivPost.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: { 
       Log.d("ACTION_DOWN", "ACTION_DOWN"); 
       break; 
      } 
      case MotionEvent.ACTION_MOVE:{ 
       Log.d("ACTION_MOVE", "ACTION_MOVE"); 
       break; 
      } 
      case MotionEvent.ACTION_CANCEL:{ 
       Log.d("LENGTH_LONG!", "LENGTH_LONG"); 
       break; 
      } 
      } 
      return true; 
     } 
    }); 
+0

您使用時ImageView的是clicked..so什麼是你最初的邏輯這是trigered setonclicklistener .. BRB對你的回答一定要回答一些家庭電話 – Elltz 2014-12-05 23:48:47

+0

我剛剛實現了像下面的代碼的「onTouch監聽器」,但我不知道下一步該怎麼做! – Icefall007 2014-12-06 00:04:18

+0

先生,深深的敬意,首先是onClick聽衆..但ima很快就回答了.. – Elltz 2014-12-06 00:06:43

回答

0
image.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getBaseContext(), "image clicked", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    image.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View arg0, MotionEvent arg1) { 
      // TODO Auto-generated method stub    
      switch (arg1.getAction()) { 
      case MotionEvent.ACTION_UP: 
       // where user releases finger 
       if(!moved){ 
        Toast.makeText(getBaseContext(), String.valueOf(moved), Toast.LENGTH_SHORT).show(); 
        image.performClick(); // you use this if you badly need the onclick listener 
        // but you can replace and put your onclick listener codes directly here 
       } 
       break; 

      case MotionEvent.ACTION_MOVE: 
       moved = true; 
       Toast.makeText(getBaseContext(), "image is moving", Toast.LENGTH_SHORT).show(); 
       Toast.makeText(getBaseContext(), String.valueOf(moved), Toast.LENGTH_SHORT).show(); // scroll your stuff here, meaning put the scrolling codes here 
       break; 

      case MotionEvent.ACTION_DOWN:     
       //this is where touch is trigered, you can call the performclick function here     
       // this will always b called first but we can do some boolean magic here.. 
       moved = false; 
       break; 
      } 
      return true; 
     } 
    }); 

你的布爾應該是一個全局變量private boolean moved; // this is the boolean

+0

請你能幫我用滾動代碼嗎? – Icefall007 2014-12-06 01:06:07

+0

我也注意到它總是在「action_move」之前調用「action_down」! – Icefall007 2014-12-06 01:07:27

+0

是啊,先生,它的觸動發佈時觸發視圖..是啊那麼你的滾動代碼如何你想它.. @ Icefall007 – Elltz 2014-12-06 01:12:51