【问题标题】:How to change the height of ExpandableListView when group is expanded扩展组时如何更改ExpandableListView的高度
【发布时间】:2014-08-08 14:08:20
【问题描述】:

分组展开/折叠时如何改变ExpandableListView的高度?

我想改变 ExpandableListView 的高度以适应其内容的高度,这样视图就不会在自身内部滚动。

这是主要活动的布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.activity.FooActivity"
    tools:ignore="MergeRootFrame" />

这是主要活动片段的布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.fragment.FooFragment"
    android:background="@color/category_bg">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout">

        <TextView
            android:text="@string/subtitle_question_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/subtitle_category"
            android:gravity="center_vertical|center_horizontal"
            android:layout_gravity="center_horizontal|top"
            android:textSize="20sp" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout"
        android:background="@drawable/category_list_bg_shape"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:minHeight="300dp">

    <LinearLayout
        android:id="@+id/category_list_progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical"
        android:visibility="gone">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <ExpandableListView
        android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/category_list"
            android:transcriptMode="normal">

        </ExpandableListView>
        </FrameLayout>

</RelativeLayout>

以下是实际布局。
ExpandableListView 内容过多时会往里面滚动。
我想滚动整个屏幕。不仅滚动视图,而且带有标题。

【问题讨论】:

  • 如果你将它的高度设置为 WRAP_CONTENT 它不应该滚动,你也不需要在展开/收缩时改变高度
  • @rupps 查看问题。我添加了我的布局。我已经尝试过 wrap_content,但这对我没有帮助:(
  • 将父框架布局高度设置为 match_parent,使其足够大。如果元素不适合,ExpandableList 将滚动。还要检查这是否是您的问题stackoverflow.com/questions/15106471/…
  • 如果您发布屏幕截图以找出您的布局会有所帮助
  • @rupps 屏幕截图已添加。

标签: android android-layout


【解决方案1】:

我解决了一个类似的问题,将所有内容放入标题中。我接近解决它的传统方式(即嵌套布局,权重,......),但我觉得解决方案很脏,需要覆盖方法,如 onGroupClick 等......来禁用 ExpandableListView 的滚动,而标题的东西很干净,对我来说完美无瑕。

因此,如果您想尝试这种方法,只需将ExpandableListView 单独放在您的片段中,然后准备将其余部分作为标题插入。

所以在你的片段中完整的ExpandableListView

 <ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/category_list"
    android:transcriptMode="normal">
 </ExpandableListView>

然后创建布局expandable_header.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.fragment.FooFragment"
    android:background="@color/category_bg">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout">

        <TextView
            android:text="@string/subtitle_question_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/subtitle_category"
            android:gravity="center_vertical|center_horizontal"
            android:layout_gravity="center_horizontal|top"
            android:textSize="20sp" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout"
        android:background="@drawable/category_list_bg_shape"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:minHeight="300dp">

    <LinearLayout
        android:id="@+id/category_list_progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical"
        android:visibility="gone">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</RelativeLayout>

然后将header充气到ExpandableListView中:

.
.
.
View view = View.inflate(context, R.layout.expandable_header, null);
expandableList.addHeaderView(view, null, false);
.
.
.

这样,标题将与 ExpandableListView 一起滚动,因为它在其中。

顺便说一句,您的布局有多余的元素,例如 FrameLayout 仅包含 TextView。您只能使用TextView。还有LinearLayoutProgressBar,如果你没有使用不透明的背景,你可以单独放置ProgressBar。您使用的ViewGroups 越少,布局的性能就越好!

【讨论】:

  • 谢谢,但就我而言,ExpandableListView 有自己的背景纹理(带边框半径的白色背景)。据我了解,如果我使用header ExpandableListView 的背景也会应用到header。所以我认为我不能使用这种方法。或者有什么解决方法吗?顺便说一句,感谢您对布局的审核!
  • ohhh ...我明白了..那么我想这对你不起作用,因为背景也将应用于标题。您可以为标题设置另一个背景,但角落不会显示在它下面...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多