【问题标题】:How to Expand and Hide Scrollviews with Button click in Android如何在 Android 中通过单击按钮来展开和隐藏滚动视图
【发布时间】:2012-07-11 14:06:34
【问题描述】:

我正在尝试创建一个有 4 个按钮的页面。

当我单击一个按钮时,该按钮应保持突出显示状态,其下方应显示一个带有文本的滚动视图,然后按下该按钮下方的按钮(必要时甚至关闭屏幕)。再次单击按钮后,滚动视图将再次隐藏。

如何让滚动视图在单击按钮时隐藏并重新出现?

这就是我想要的样子:http://img407.imageshack.us/img407/3448/47163794.png

【问题讨论】:

  • 听起来你应该使用 ExpandableListView。
  • 你不能使用可见性标志吗?
  • ExpandableListView 可能是我正在寻找的更多内容,有什么好的教程或示例吗?我很难找到它们

标签: android button hide scrollview expand


【解决方案1】:

这是我最近对ExpandableListView 所做的事情。这相当容易。

它拥有迄今为止我在 android 中见过的最大的构造函数之一。


使用ExpandableListView 创建一个布局。

<LinearLayout>

    <ExpandableListView>
        <!-- Set the properties, if you need to see them, let me know. -->
    </ExpandableListView>

</LinearLayout>

现在在某些视图中使用布局。

public void onCreate() {

    ExpandableListView example = findViewById(/* The id of the expandable list view */);


    example.setAdapter(
            new SimpleExpandableListAdapter(
                        /* the context */,
                        /* the map of parents/groups */,
                        /* the layout to map the parents/groups to */,
                        /* the key to search for in the parents/groups map */,
                        /* the id of the textview inside the layout to map the parent/group to */,
                        /* the map of children */,
                        /* the layout to map the children to */,
                        /* the key to search for in the children map */,
                        /* the id of the textview inside the layout to map the children to */)
            )
}

还有其他可用的适配器,但这是最基本的。

基本上只看developer website。这个example 也有点帮助,但是开发者的网站还有很长的路要走。

如果您有任何问题,请告诉我。


跟进

他放了 createGroupList()。看起来这是他评论的功能 出去。这到底是在做什么?

他基本上是在该函数中创建HashMapsList 并返回创建的对象。是的,您应该这样做,它有助于使您的代码更简洁。

此外,这些列表显示得很小,无论如何要让它们变大?

修改您的TextView,以显示您的孩子和组。在 xml 布局中为 View 添加填充。只需调整dp。您可以增加textSize 或者您可以添加style,如下所示。

<TextView 
        android:id="@+id/groupTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/TextAppearance.Medium"
        android:paddingLeft="50dp"
        android:paddingTop="25dp"
        android:paddingBottom="25dp"/>

我希望父组是列表,但不希望子组 是子列表,我希望它们成为我可以添加文本的文本框(我 从网上抢)。我如何制作儿童文本框而不是 列表。

您可能需要针对这种情况创建自己的Adapter。您需要扩展BaseExpandableListAdapter。我可能是错的,可能有更简单的方法......但我不知道。


额外的跟进

不确定你的意思。在子列表中输入的文本 出现在这行代码中:child.put("Sub Item", "test")。我能 把我想要的文字放在写着“测试”但“测试”出现在下面的地方 每个父列表。

您的createChildList 需要返回一个List&lt;ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt;

您的createChildList 应该如下所示:

private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) {

                ArrayList<ArrayList<HashMap<String, String>>> groupList = 
                                new ArrayList<ArrayList<HashMap<String, String>>>();

                for (int i = 0; i <= maxValue; i++) {
                        ArrayList<HashMap<String, String>> childList = 
                                        new ArrayList<HashMap<String, String>>();

                        for (int j = 0; j <= maxValue; j++) {
                                HashMap<String, String> map = new HashMap<String, String>();

                                map.put("Sub Item", "test: " + String.valueOf(j));
                                childList.add(map);
                        }

                        groupList.add(childList);
                }

                return groupList;
        }

让我知道这是否有效。

【讨论】:

  • 嘿,这真的很有帮助,谢谢。有几件事,我点击了那个例子的链接,它很好,但是对于 /* 父母/组的地图 */ 部分,他把 createGroupList().看起来这是他注释掉的功能。这究竟是在做什么?我应该使用他注释掉的函数吗?抱歉,我对此有点陌生,非常感谢您的帮助
  • 我想我真的想通了。还有两个问题,我希望父组成为列表,但不希望子组成为子列表,我希望它们成为我可以添加文本的文本框(我从网上获取)。我如何制作子文本框而不是列表。此外,这些列表显示得很小,无论如何要使它们更大?谢谢!
  • 在我的回答中添加了更多关于您在 cmets 中的问题。如果有帮助,请不要忘记 +1 并接受答案。
  • 非常感谢!这真的帮助了我。由于我对适配器不是非常熟悉,因此我将在其他地方搜索文本框问题。
  • 没问题。祝你好运,如果您找到更简单的解决方案,请在此处发表评论。我对你的发现很感兴趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多