1、需要继承OnGestureListener和OnDoubleTapListener,如下:
Java代码
public class ViewSnsActivity extends Activity implements OnTouchListener, OnGestureListener
2、在添加mGestureDetector的定义,并在ViewSnsActivity的onCreate函数中加入其页面布局的setOnTouchListener事件
Java代码
GestureDetector mGestureDetector;
Java代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_sns_activity);
mGestureDetector = new GestureDetector((OnGestureListener) this);
LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.viewSnsLayout);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
}
3、重载onFling函数
Java代码
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX()-e2.getX() > snsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
// 切换Activity
// Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
// startActivity(intent);
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} else if (e2.getX()-e1.getX() > snsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
// 切换Activity
// Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
// startActivity(intent);
Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
}
return false;
}
其中SnsConstant如下:
Java代码
public class SnsConstant {
private static final int FLING_MIN_DISTANCE = 50;
private static final int FLING_MIN_VELOCITY = 0;
public static int getFlingMinDistance() {
return FLING_MIN_DISTANCE;
}
public static int getFlingMinVelocity() {
return FLING_MIN_VELOCITY;
}
}
4、重载onTouch函数
Java代码
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
使用UIScrollView应该就可以实现类似这种效果
左划返回的功能吗?可以用xposed框架有个滑动返回模块
viewpager+fragment可以实现左右切换
可以使用 android ViewPages 控件;