如何設置按鈕以偵聽長按事件,其中按鈕位於已使用手勢檢測器偵聽onFling事件的視圖上?Android中的多個偵聽器
我已經添加了參考代碼。請解釋我如何在一個活動中實現多個監聽器。另外,多個監聽器可以爲單個視圖實現嗎?其中之一是一個長按聽衆,而另一個是onfling聽衆
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.knowledge);
summary =(TextView)findViewById(R.id.textSummary);
buckview = (View) findViewById(R.id.bucketView);
ball = (View)findViewById(R.id.meatBall);
gestureDetector = new GestureDetector(new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
/*
//Set touch release listener for bucket view
mainview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
{
buckview.setVisibility(RelativeLayout.GONE);
}
return false;
}
});
*/
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
ball.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
buckview.setVisibility(ImageView.VISIBLE);
return false;
}
});
ball.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_UP)
{
buckview.setVisibility(ImageView.GONE);
return false;
}
else
return true;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
public void onShowPress(MotionEvent event){
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Intent intent = new Intent(Knowledge.this.getBaseContext(), Knowledge.class);
/* If only horizontal allowed
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
*/
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_left
);
return false;// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_right
);
return false;// top to bottom swipe
} else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_top,
R.anim.slide_out_bottom
);
return false;// bottom to top swipe
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
startActivity(intent);
Knowledge.this.overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_top
);
return false;
}else
return true;
}
// It is necessary to return true from onDown for the onFling event to register
@Override
public boolean onDown(MotionEvent event) {
return true;
}
}
}
該按鈕應該仍然工作完美..告訴我們你試過..和你的結果.. – amalBit
我試着用2個不同的聽衆。主視圖的手勢檢測器和按鈕的onlogclicklistener。但是,這並沒有解決。 –
當你設計的XML。 使用框架佈局並將按鈕放在單獨視圖中 – amalBit