如何在组展开/折叠时改变ExpandableListView的高度?
我想更改 ExpandableListView 的高度以适应其内容的高度,这样 View 就不会在其内部滚动。
这是主要 Activity 的布局。
<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" />
这是主要 Activity fragment 的布局。
<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 内容过多时会向内滚动。
我想滚动整个屏幕。不仅是 ScrollView ,还有标题。
最佳答案
我解决了一个类似的问题,将所有内容都放入标题中。我接近于用传统方法(即嵌套布局、权重等)解决它,但我觉得这个解决方案很脏,需要重写方法,例如 onGroupClick 等...来禁用 ExpandableListView 的滚动,而标题的东西很干净,对我来说完美无瑕。
因此,如果您想尝试这种方法,只需将 ExpandableListView
单独放入您的 fragment 中,并准备将其余部分作为页眉插入。
因此在您的 fragment 中有一个完整的 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>
然后将标题膨胀到 ExpandableListView 中:
.
.
.
View view = View.inflate(context, R.layout.expandable_header, null);
expandableList.addHeaderView(view, null, false);
.
.
.
这样,标题将与 ExpandableListView
一起滚动,因为它位于其中。
顺便说一句,您的布局包含冗余元素,例如仅包含 TextView
的 FrameLayout
。您只能使用 TextView
。还有 LinearLayout
和 ProgressBar
,如果您不使用不透明背景,您可以单独放置 ProgressBar
。您使用的 ViewGroups
越少,布局的性能就越好!
关于android - 展开组时如何更改ExpandableListView的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25205466/