2014-12-05 79 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