实现类似主屏幕的水平滚动小部件?
大多数 Android 设备都有一个主屏幕,您可以通过拖动来水平滚动(还有其他设备也具有类似网格的 2d 滚动功能,但我只对水平滚动感兴趣)。
有人可以告诉我如何实现这一目标吗?
首先我考虑使用自定义 ViewFlipper。我可以为其附加适当的滑动动画,但这并不完全相同。在主屏幕上,“滑动量”取决于您在屏幕上拖动手指的距离。动画是一劳永逸的东西,我无法中途阻止它们。
有什么想法吗?
以下是有关所需行为的视频:http://www.youtube .com/watch?v=UGdWM2TQNSo&feature=player_detailpage#t=104s
Most Android devices feature a home screen which you can scroll horizontally by dragging (there're others that have a grid-like 2d scrolling feature too, but I'm only interested in horizontal scrolling).
Could someone shed some light for me on how this might be achieved?
First I was thinking about using a custom ViewFlipper. I could attach proper sliding animations for it, but it's not exactly the same thing. On the home screen, the "amount of sliding" depends on how far you've dragged your finger on the screen. Animations are fire-and-forget things, I can't stop them midway.
Any ideas?
Here's a video about the desired behaviour: http://www.youtube.com/watch?v=UGdWM2TQNSo&feature=player_detailpage#t=104s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处查看 Android 的原生启动器应用程序执行此操作的方式:https://android.googlesource.com/platform/packages/apps/Launcher2/+/master/src/com/android/launcher2/Workspace.java
您主要对
onTouchEvent
、onInterceptTouchEvent
和computeScroll
方法感兴趣。You can take a look at the way Android's stock Launcher app does this here: https://android.googlesource.com/platform/packages/apps/Launcher2/+/master/src/com/android/launcher2/Workspace.java
You're primarily interested in the
onTouchEvent
,onInterceptTouchEvent
, andcomputeScroll
methods.Google 发布了包含一些新 UI 元素的兼容包,您可以通过 SDK Manager 下载。
将位于
sdkfolder/extras/compatibility
中的 .jar 文件放入构建路径中。现在您可以使用名为
ViewPager
的类,其行为类似于 Android 主屏幕。或者,如果您使用 Eclipse 和 ADT 插件 v12 或更高版本,只需右键单击您的项目,选择“Android 工具”,然后选择“添加兼容性包”。
Google released the compatibility pack with some new UI Elements, which you can download via SDK Manager.
put the .jar file located in
sdkfolder/extras/compatibility
in your build path.now you can use a class called
ViewPager
which behaves like the Android Homescreen.Or, if you are using Eclipse and ADT plugin v12 or higher, just right click your project, choose "Android Tools" and then "add compatibility package".