Android 当用户在屏幕上滑动手指时,如何让我的页面滑动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2697799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make my page slide as the user slides finger on the screen?
提问by Muhammad Maqsoodur Rehman
How can I make my page slide as the user slides finger on the screen? Any example code?
当用户在屏幕上滑动手指时,如何让我的页面滑动?任何示例代码?
I just require the same feel as it is on my android g-phone's home screen. The screen moves as the finger moves (also includes the elastic effect).
我只需要与我的 android g-phone 主屏幕上相同的感觉。屏幕随着手指的移动而移动(也包括弹性效果)。
采纳答案by Steve Haley
Check out this tutorialand its follow upon warriorpoint. They explain how to use the ViewFlipper to smoothly animate the transition from one screen to another, and then in the second part how to do this using touch control. Note, these are whole-screen transitions not panning around on an existing page. For panning, e.g. on an oversized image that doesn't fit in the screen, check out Android BigImage. Depending on what you're trying to do these might be overkill, but it's not 100% clear what you're trying to achieve.
看看这个教程和及其后续行动上warriorpoint。他们解释了如何使用 ViewFlipper 平滑地从一个屏幕到另一个屏幕的过渡动画,然后在第二部分中如何使用触摸控制来做到这一点。请注意,这些是全屏转换,而不是在现有页面上平移。对于平移,例如在不适合屏幕的超大图像上,请查看Android BigImage。根据您尝试执行的操作,这些可能有点矫枉过正,但您要实现的目标并不是 100% 清楚。
回答by ludwigm
回答by Bob
ViewPager. This would seem to have been addressedmore completely after the fact:
Whether you have just started out in Android app development or are a veteran of the craft, it probably won't be too long before you'll need to implement horizontally scrolling sets of views. Many existing Android apps already use this UI pattern, such as the new Android Market, Google Docs and Google+. ViewPager standardizes the implementation.
无论您是刚开始从事 Android 应用程序开发还是已经是该领域的老手,用不了多久您就需要实现水平滚动的视图集。许多现有的 Android 应用程序已经使用这种 UI 模式,例如新的 Android Market、Google Docs 和 Google+。ViewPager 对实现进行了标准化。
回答by Diego Palomar
The Home screen is made of two layers:
主屏幕由两层组成:
The background (or Workspace) that shows the image; this layer scrolls and draws the wallpaper with a different scroll value. Workspace behaves more or less like a simplified horizontal layout
The screens (or CellLayouts) that display the icons; this layer is made of 3 CellLayout side by side. CellLayout is a custom grid-like layout.
显示图像的背景(或工作区);此图层滚动并使用不同的滚动值绘制墙纸。工作区的行为或多或少像一个简化的水平布局
显示图标的屏幕(或 CellLayouts);这一层由 3 个 CellLayout 并排组成。CellLayout 是一个自定义的类似网格的布局。
The fling is implemented using a VelocityTracker, a Scroller and regular View.scroll methods.
使用 VelocityTracker、Scroller 和常规 View.scroll 方法实现投掷。
回答by MrCloister
I did this for my application, Just thought I'd share my solution.
我为我的应用程序做了这个,只是想我会分享我的解决方案。
回答by Kantesh
use gallery view
使用画廊视图
回答by Gaurav Vaish
You will need Gesture detector - the classes / interfaces under considerations are:
您将需要手势检测器 - 正在考虑的类/接口是:
1. android.view.GestureDetector
2. android.view.GestureDetector.SimpleOnGestureListener
Method of interest is onFling
.
感兴趣的方法是onFling
。
Now, based on the X, Y and the velocity in respective directions, you can "draw your view" / "reposition the nested views in case of activity or view-group" at corresponding location.
现在,基于 X、Y 和各个方向的速度,您可以在相应位置“绘制您的视图”/“在活动或视图组的情况下重新定位嵌套视图”。
An example of how to redraw can be found in any game-example like LunarLander in Android at http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/LunarView.html
如何重绘的示例可以在 Android 中的 LunarLander 等任何游戏示例中找到,网址为http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/LunarView.html