我想在屏幕顶部添加幻灯片进行搜索。我的意思是当用户滑动/滑动放置在屏幕顶部的图像时,它应该将用户引导到另一个屏幕,但是当用户滑动一点时它应该回到它自己的位置。
//代码
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
((Button) mainHomeView.findViewById(R.id.btn_home_small_start)).setOnClickListener(HomeActivity_1.this);
((Button) mainHomeView.findViewById(R.id.btn_home_small_start)).setOnTouchListener(gestureListener);
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
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) {
Toast.makeText(HomeActivity_1.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
runOnUiThread(new Runnable() {
@SuppressLint("NewApi")
public void run() {
((Button) findViewById(R.id.btn_home_small_start)).animate().translationX(100.0f);
}
});
Toast.makeText(HomeActivity_1.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}