我正在尝试在我的应用程序的页面之间实现滑动。我目前有 5 个 XML 布局文件,我可以从一个 Activity 访问所有这些文件。底部有五个按钮,代表每个布局,按下时切换到相应的 XML 布局。
我曾尝试使用 来实现刷卡OnGestureListener
,但它并没有像我希望的那样工作。我需要滑动Title
视图才能使滑动工作。当我在布局中显示的任何视图上滑动时,它不起作用。
我开始寻找并找到了GestureOverlayView
,这似乎是我想要的。但是,我发现的所有实现都给我带来了问题。教程,类似的 SO 问题。
这是我到目前为止的代码:
public class Monitor extends Activity implements android.view.GestureDetector.OnGestureListener, OnGesturePerformedListener {
private GestureDetector gestureScanner;
private GestureOverlayView gestures;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
private GestureLibrary gestureLib;
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
gestures = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestures.addView(inflate);
gestures.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); // Error here
if (!gestureLib.load()) {
finish();
}
setContentView(gestures);
}
我在线上遇到错误gestureLib = GestureLibraries.fromRawResource();
。错误是raw cannot be resolved or is not a field.
。raw
我的目录中没有文件夹/project-name/res/
。有任何想法吗?还有另一种方法可以做到这一点吗?
这就是我第一次实现滑动功能的方式,它只在标题栏上起作用。
public class Monitor extends Activity implements android.view.GestureDetector.OnGestureListener {
private GestureDetector gestureScanner;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
gestureScanner = new GestureDetector(this);
setContentView( R.layout.monitor );
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i( "TouchEvent", "Here" );
return gestureScanner.onTouchEvent(event);
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
Log.i( "OnFling", "Right" );
//ur code goes here.
if( getTitle().equals( getString( R.string.main_analogs ) ) ) {
setContentView( R.layout.monitor );
setTitle( R.string.main_monitor );
buildButtons();
if( hasRead ) {
updateMonitorForm();
}
}
}
/* on scroll to the previous page */
else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
Log.i( "OnFling", "Left" );
//Ur code goes here
if( getTitle().equals( getString( R.string.main_monitor ) ) ) {
setContentView( R.layout.analogs );
setTitle( R.string.main_analogs );
buildButtons();
if( hasRead ) {
updateMonitorForm();
}
}
}
return false;
}