安卓:列表视图中滚动型与动力高度 [英] Android: Listview in ScrollView with dynamic height
问题描述
目前我正在开发一个滚动视图设置菜单。其基本结构如下所示:
currently I am developing a settings menu in a scrollview. The basic structure looks like the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollview_settings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/paddingSmall">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent_white_settings"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium">
<ImageView
android:id="@+id/logo"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:src="@drawable/newspaper_logo_color"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/paddingBig"
android:adjustViewBounds="true"/>
</RelativeLayout>
<!-- Layout News Uebersicht -->
<include layout="@layout/separator_line_grey"/>
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="@color/buttonHoverBackground"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:paddingLeft="@dimen/paddingLarge"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium"
android:text="@string/news_overview"
android:textScaleX="@dimen/letter_spacing"
android:textStyle="bold"
android:gravity="center_vertical"
android:background="@color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="@layout/separator_line_grey"/>
<!-- News-Overview Issues -->
<LinearLayout
android:id="@+id/issue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/transparent_white_settings">
<de.basecom.noznewsapp.views.NewsScrollListView
android:id="@+id/listview_issues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:divider="@null"
android:dividerHeight="0dp" />
</LinearLayout>
<!-- Layout Einstellungen -->
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="@color/buttonHoverBackground"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:paddingLeft="@dimen/paddingLarge"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium"
android:text="@string/settings"
android:textStyle="bold"
android:textScaleX="@dimen/letter_spacing"
android:gravity="center_vertical"
android:background="@color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="@layout/separator_line_grey"/>
<!-- Einstellungs- Werte -->
<LinearLayout
android:id="@+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/transparent_white_settings">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/paddingLarge">
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/receive_pushmessages"
android:textAllCaps="true"
android:textScaleX="@dimen/letter_spacing"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:layout_centerVertical="true"
android:textColor="@color/buttonBackground"/>
<Switch
android:id="@+id/receive_pushmessages_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/empty_string"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
有关我用一个自定义实现ListView控件,给它一个固定的高度,使其能够滚动视图(注意内滚动:我必须使用滚动视图中的列表视图,因为我有很多的元素,因此使用一个的LinearLayout不会很好)。
For the listview I used a custom implementation, to give it a fix height and make it able to scroll within the scrollview (note: I have to use the listview within the scrollview, because I have lots of elements, so that using a linearlayout wouldn't be nice).
public class CustomListView extends ListView {
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
列表视图中的每个项目包含一个标签和一个gridview,这是所有正确显示。但现在我想要实现的功能,用户可以在标签上点击显示和隐藏GridView控件的可见性。
Each item of the listview contains a label and a gridview, which is all shown correctly. But now I want to implement the functionality, that the user can click on the label to show and hide the visibility of the gridview.
但是,如果我单击标签和GridView控件显示,列表视图的高度并不适应新的开放元素。它总是保持在相同的高度,如果我开的元件,下面的元件不再可见。这也即使发生了,如果我叫列表视图的 requestLayout()
方法来触发其 onMeasure
试。
But if I click on the label and the gridview is shown, the height of the listview doesn't adapt to the new open element. It always stays at the same height and if I open an element, the elements below are no longer visible. This also even happens, if I call the requestLayout()
method of the listview to trigger its onMeasure
again.
任何人有一个想法是什么,我做错了?
Anybody got an idea what I am doing wrong?
编辑:更新我的xml文件:)
Updated my xml file :)
推荐答案
最后,我只能通过更换来解决这个问题列表视图
与的LinearLayout
并添加我的code项目布局。
Finally I was only able to solve this issue by replacing Listview
with a LinearLayout
and add my items in code to the Layout.
这篇关于安卓:列表视图中滚动型与动力高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!