我希望能够点击并滚动图像视图,但每当我触摸图像视图并执行"动作移动时,就会发生这种情况。它不滚动但执行"点击事件"代替! 我希望能够做到这两个意思,当我触摸和移动它应该滚动,但当我只是触摸然后它应该执行点击! 提前致谢
这就是我所做的
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 :(得分: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