Android - 手风琴小工具

时间:2009-07-21 22:03:47

标签: android widget accordion

我正在寻找创建Accordion-style widget such as on this page的最佳方法。 有没有办法使用标准的Android工具包实现相同的效果,还是我需要构建自定义小部件?如果是的话 - 如果有的话,你会推荐哪一个?

4 个答案:

答案 0 :(得分:28)

如果您仍然想知道 - 这可以通过堆叠在线性布局内部的按钮/布局对完成。伪代码如下

<LinearLayout android:orientation="vertical">
    <Button android:text="Panel 1"/>
    <SomeKindOfLayout android:id="@+id/panel1">
            <!-- widgets in first panel go here -->
    </SomeKindOfLayout>
    <Button android:text="Panel 2"/>
    <SomeKindOfLayout android:id="@+id/panel2" android:visibility="gone">
            <!-- widgets in second panel go here -->
    </SomeKindOfLayout>
    <Button android:text="Panel 3"/>
    <SomeKindOfLayout android:id="@+id/panel3" android:visibility="gone">
            <!-- widgets in third panel go here -->
    </SomeKindOfLayout>
    <Button android:text="Panel 4"/>
    <SomeKindOfLayout android:id="@+id/panel4" android:visibility="gone">
            <!-- widgets in fourth panel go here -->
    </SomeKindOfLayout></LinearLayout>

可能尝试的另一件事是将ExpandableListView-s堆叠在彼此之上

答案 1 :(得分:22)

我在github上推了android accordion view项目。我们将它用于我们的客户,在2.2,2.3.x,3.2和4.0.3上进行测试。对我们来说非常有用。

下一步要在fold /展开时添加动画。

这是一个小截图:

enter image description here

答案 2 :(得分:4)

我必须冒险并说@Bostone答案是最合适的。我会在下面给你我的代码,你可以看到。感谢GitHubMaciejŁopaciński,但正如@SudoPlz在评论中所说,根本没有文档。我不知道从哪里开始。我试着用Eclipse和Android Studio来做这件事,我既不能运行项目来开始弄清楚它是如何工作的。此外,该项目的最后一次提交大约是1年前,这让我很害怕,如果出现问题,我将陷入死胡同。所以没有进一步的延迟,这是我在@Bostone的解决方案:

1.-首先,您必须创建一个嵌套在ScrollView中的Button和Layout:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TasksListActivity">


            <Button
                android:id="@+id/magic_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Magic Btn"/>
            <LinearLayout
                android:id="@+id/magic_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">

        </LinearLayout>
</ScrollView>

2.-之后转到相应的JAVA,找到按钮并设置onClickListener,在onClickListener中你会找到布局。

Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
        findMagicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

3.-所以现在在onClickListener中我们会找到布局。正如您在第一步中看到的那样,布局默认设置为GONE,但现在我们必须将其设置为可见。问题是,如何让它再次消失,反之亦然?因此,我们将添加条件以获取布局的可见性,并基于它将被隐藏或显示:

Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
        findMagicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout findMagicLl = (LinearLayout) findViewById(R.id.magic_layout);
                if (findMagicLl.getVisibility() == View.VISIBLE) {
                    findMagicLl.setVisibility(View.GONE);
                } else {
                    findMagicLl.setVisibility(View.VISIBLE);
                }
            }
        });

这可以多次相互叠加。

所以现在,如果你真的想让它看起来像手风琴,你可以拉开按钮。你可以在它的右边放一个箭头,例如:

findMagicBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right_arrow, 0);

您应该在onClickListener中执行此操作并使用条件更改箭头的方向,方法是更改​​drawable(如果是向下和向下箭头,如果是可见的向上箭头)。

此外,您可以通过添加动画使其看起来更自然,如下所示:

ScaleAnimation animation = new ScaleAnimation(1f, 1f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(180);
animation.setFillAfter(true);
findMagicLl.startAnimation(animation);

您必须考虑在视图可见时必须完成上述动画。请注意,setFillAfter(true)会永久更改容器的大小,如果您这样做,可能您不想再使用VIEW.GONE了。这个问题对Scale Animation

有很好的解释

答案 3 :(得分:2)

在我的应用程序中,我使用了Riya Gayasen的手风琴视图。入门和运作良好非常容易。这是我所做的。

  1. 从以下位置下载源代码的zip https://github.com/riyagayasen/Android_accordion_view
  2. 解压缩“ easyaccordion”文件夹并将其复制到项目的根目录 目录(包含“ app”文件夹的目录)
  3. 在项目根目录settings.gradle文件中添加:easyaccordion。这里 我的文件是这样的: include ':app',':easyaccordion'
  4. 在您的app / build.gradle文件中,将以下行添加到 依赖项块。 implementation 'com.riyagayasen.android:easyaccordion:1.0.3'
  5. 就是这样。您可以开始使用内部的组件 布局。
相关问题