Android - 手风琴小部件
我正在寻找创建 手风琴风格小部件的最佳方法,例如此页面。 有没有办法使用标准 Android 工具包达到相同的效果,还是我需要构建自定义小部件? 如果是这样 - 如果有的话,您会建议延长哪一项?
I'm looking at best way of creating an Accordion-style widget such as on this page.
Is there a way of achieving same effect using standard Android toolkit or do I need to build custom widget? If so - which one would you recommend extending if any?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您仍然想知道 - 这几乎可以通过堆叠在线性布局内部的一对按钮/布局来完成。 伪代码如下
另一件可能尝试的事情是将 ExpandableListView 堆叠在一起
And in case you still wonder - this can be pretty much done with pair of button/layout stacked inside of the linear layout. Pseudo code follows
Another thing to possibly try is stacking ExpandableListView-s on top of each other
我已经在 github 上推送了 android 手风琴视图 项目。 我们为客户使用它,并在 2.2、2.3.x、3.2 和 4.0.3 上进行了测试。 对我们来说效果很好。
将在下一步中添加折叠/展开动画。
这是小屏幕截图:
I have pushed android accordion view project at github. We use it for our customers, tested on 2.2, 2.3.x, 3.2 and 4.0.3. Works pretty good for us.
Going to add animation on fold/unfold in next step.
Here is small screenshot:
我必须冒着风险说@Bostone 的答案是最合适的。 我将在下面给你我的代码,以便你可以看到。 感谢 GitHub Maciej Łopaciński,但正如 @SudoPlz 在评论中所说,根本没有文档。 我不知道从哪里开始。 我尝试使用 Eclipse 和 Android Studio 来完成此操作,但我都无法运行该项目来开始弄清楚它是如何工作的。 此外,该项目的最后一次提交是大约一年前,这让我很担心如果出现问题我会陷入死胡同。 因此,不再拖延,这是我基于 @Bostone 的解决方案:
1.- 首先,您必须创建一个嵌套在 ScrollView 中的按钮和布局:
2.- 之后转到相应的 JAVA,找到该按钮并设置一个onClickListener,在 onClickListener 中您将找到布局。
3.- 现在在 onClickListener 中我们将找到布局。 正如您在第一步中看到的,布局默认设置为 GONE,但现在我们必须将其设置为可见。 问题是,我怎样才能让它再次消失,反之亦然? 因此,我们将添加一个条件来获取布局的可见性,并根据该条件隐藏或显示布局:
可以将其相互重叠任意多次。
所以现在,如果你真的想让它看起来像手风琴,你可以修改按钮。 您可以在其右侧放置一个箭头,例如:
您应该在 onClickListener 内部执行此操作,并使用条件通过更改可绘制对象来更改箭头的方向(如果消失和向下箭头,并且如果可见向上箭头)。
另外,您可以通过添加动画使其看起来更自然,如下所示:
您必须考虑到上述动画必须在视图可见时完成。 请注意,
setFillAfter(true)
将永久更改容器的大小,因此如果您这样做,也许您不想再使用VIEW.GONE
。 这个问题对缩放动画有一个精彩的解释I have to risk myself and say that the @Bostone answer is the most appropriate. I will give you my code below so you can see. Thanks for the GitHub Maciej Łopaciński, but as @SudoPlz said in the comments, there is no documentation at all. I didn't knew where to start. I try to do it with Eclipse and Android Studio and in neither I could run the project to start figuring out how its work. Besides, the last commit in that project was about 1 year ago, wich makes me fear a lot that if something goes wrong I will be stuck in a dead end. So without further delay, here is my solution based in @Bostone:
1.- First you have to create a Button and a Layout nested in a ScrollView:
2.- After that go to your corresponding JAVA, there find the button and set an onClickListener, inside the onClickListener you will find the layout.
3.- So now inside the onClickListener we will find the layout. As you can see in the first step the layout is set by default to
GONE
, but now we have to set it to visible. The problem is, how can I make it gone again and vice-versa? So we will add a condition for getting the visibility of the layout and based on that it will be hidden or show up:This can be put over each other as many times you want.
So now if you really want to make it look like an accordion you can pimp the button. You could put an arrow on the right of it, using by example:
You should do that inside the onClickListener and use the condition to change the direction of the arrow, by changing the drawable (if is gone and down arrow, and if is visible an up arrow).
Also, you can make it look more natural by adding an animation, like this:
You will have to consider that the above animation must be done while the view is visible. Please be aware that
setFillAfter(true)
, will change the size of the container permanently so if you do so, maybe you don't want to useVIEW.GONE
anymore. This question has a wonderfull explanation about the Scale Animation在我的应用程序中,我使用了 Riya Gayasen 的手风琴视图。 上手非常容易并且效果很好。 这就是我所做的。
https://github.com/riyagayasen/Android_accordion_view。
目录(包含“app”文件夹的目录)
我的文件是这样的:
include ':app',':easyaccordion'
依赖块。 <代码>
实现 'com.riyagayasen.android:easyaccordion:1.0.3'
布局。
In my app, I used Riya Gayasen's accordion view. It was very easy to get started and worked well. Here is what I did.
https://github.com/riyagayasen/Android_accordion_view.
directory (the directory containing the 'app' folder)
is how my file looks like:
include ':app',':easyaccordion'
dependencies block.
implementation 'com.riyagayasen.android:easyaccordion:1.0.3'
layout.