子类化 Gallery 小部件以创建“Ticker”行为

发布于 2024-12-07 20:45:02 字数 673 浏览 0 评论 0原文

这主要是一个哲学问题,因为实际问题已经解决了。 SDK提供了Gallery类。这个画廊满足了我 90% 的需要,只是缺少两件事。一是我想实现一个额外的 Runnable 来触发定期前进到下一个项目,二是我需要它来为项目之间的滚动设置动画。

自动定期前进很容易。然而,我遇到了在项目之间实现滚动动画的主要问题。现在我已经通过重写 setSelection 来解决这个问题,使其成为 onFling 的包装器。

目前我的背景主要是Python,所以这个实现对我来说有点肮脏。我根据父级宽度计算速度,然后调用 onFling(null, null, my_calculated_velocity, 0)。这对我来说真的是错误的,如果由于某种原因谷歌计算速度距离的方式发生变化怎么办?

现在我浏览了 Gallery 的源代码,发现了很多让我感觉好多了的方法: 移动下一个 滚动到子项 mFlingRunnable.startUsingDistance

都不能从 Gallery 的子类访问。在Python中,没有真正的私有概念,所以我发现当我在java中子类化某些东西时,我最终不得不复制粘贴该类的2/3,只是为了覆盖单个行为,这让我感到非常沮丧。

所以问题是,我是否做错了什么,导致我尝试以违反 Java 粒度的方式做事,或者每次我坐下来处理 Android 项目并找到所有可行的方法时,我是否会感到沮丧?我需要什么是私有的或默认的,而不是公开的或受保护的?

This is mostly a philosophy question, as the actual problem is solved. The SDK provides a Gallery class. This Gallery is 90% of what I need, it's just missing 2 things. One I want to implement an extra Runnable that triggers periodic advances to the next item, and two I need it to animate the scroll between items.

The auto periodic advance was easy. However I came across major issues implementing the scoll animation between items. Now I've solved this by override setSelection to essential be a wrapper to onFling.

My background is primarily Python at this point, so this implementation feels kinda dirty to me. I'm calculating a velocity based on the parents width and then calling onFling(null, null, my_calculated_velocity, 0). This feels really wrong to me, what if for some reason the way google calculates distance from velocity changes?

Now I look through the source of Gallery, and I see tons of ways that feel a ton better to me:
moveNext
scrollToChild
mFlingRunnable.startUsingDistance

None of which are accessible from a subclass of Gallery. In Python there is no real concept of private, so I find it frustrating to no end that when I subclass something in java I end up having to copy paste 2/3's of the class just to override a single behavior.

So the question is am I doing something wrong that's leading me to attempt to do things in ways that are against the Java grain or can I just expect to be frustrated everytime I sit down to work on an android project and find all the methods that do what I need to be be private or default, rather than public or protected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-12-14 20:45:02

正如前面提到的,Gallery 并不是一个很好扩展的类。实际上,更好的方法就是获取源代码并使用它。

仅供参考,如果设备由于某种原因暂时锁定,速度会发生变化。

你没有做错什么,而是这个类设计错误/为其他事情设计的。

Gallery is as previously mentioned not a very nice class to extend. A better way here would actually be just getting the source and using that.

FYI the velocity DO change, if the device for some reason locks up momentarily.

You are not doing something wrong, it's the class that is designed wrong/for something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文