ios 的左滑切换页面 android怎么实现

2024-11-29 15:32:14
推荐回答(5个)
回答1:

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);
}

回答2:

使用UIScrollView应该就可以实现类似这种效果

回答3:

左划返回的功能吗?可以用xposed框架有个滑动返回模块

回答4:

viewpager+fragment可以实现左右切换

回答5:

可以使用 android ViewPages 控件;