Android - 拖动屏幕导航
问题描述:
我必须在Android中进行拖动导航。我添加了拖动屏幕导航的代码。这不像Android默认那样流畅。下面的代码给出了从左到右和从右到左的拖动导航,但问题是当你点击右侧然后到左侧时,屏幕也会导航。什么是实现它的正确途径。我是否需要使用相同的代码来计算X值?
下面的代码。Android - 拖动屏幕导航
// On Drag Navigation
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// Going Backward: pushing stuff to the right
if (downXValue < currentX)
{
Intent lIntent = new Intent(getApplicationContext(), previousScreen.class);
startActivity(lIntent);
finish();
}
// Going Forward: pushing stuff to the left
if (downXValue > currentX)
{
Intent lIntent = new Intent(getApplicationContext(), nextScreen.class);
startActivity(lIntent);
finish();
}
break;
}
}
}
请指定实现它的正确方法。
在此先感谢
答
你可能不希望使用单独的活动这一点。此模式用于在同一活动内导航。
有两个或三个州要考虑取决于你如何选择考虑它。第一种是当用户在屏幕上有手指并来回平移时的“拖动”状态。第二和第三是用户放手时发生的事情。如果放开时的速度低于某个阈值,(系统使用ViewConfiguration中的最小速度)为最接近的屏幕添加动画。如果速度超过该阈值,则在该方向上跳到下一个屏幕。
查看VelocityTracker,Scroller和Interpolator类。他们都可以帮助你。