问题
On my fragment layout I have a Nested Scroll view with two relative layout inside, top and bottom, on the bottom I have a listview with about twenty elements, but if I set the height of the listview with wrap_content, only one line is shown, whereas if I set the height to 1000dp, for example, all the lines are shown, I wouldn't want to fix the height in a static way but I'd rather use wrap_content, what can I do?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nested_content"
android:clipToPadding="false"
android:scrollbars="none"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
//some button and text view
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom"
android:layout_below="@id/top">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scores"
</ListView>
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
回答1:
You can use below Utility method to set the height of your ListView
based on child count.
public static void setListViewHeightBasedOnChildren(ListView myListView) {
ListAdapter adapter = myListView.getAdapter();
if (myListView != null) {
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View item= adapter.getView(i, null, myListView);
item.measure(0, 0);
totalHeight += item.getMeasuredHeight();
}
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (adapter.getCount() - 1));
myListView.setLayoutParams(params);
}
}
回答2:
1.Remove the NestedScrollView
2. add this to the android:transcriptMode="alwaysScroll" like this
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scores"
android:transcriptMode="alwaysScroll"
</ListView>
回答3:
You can check out this one Expandable Height List View.
I personally use this one: Exapandable Height GridView and is the same philosophy with the Expandable Height List View.
回答4:
Man, this is one kind of RelativeLayout Blitzkrieg. But if it is what you need to fit your feature, maybe you should try to set the NestedScrollView to fill its viewport.
android:fillViewport="true"
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nested_content"
android:clipToPadding="false"
android:scrollbars="none"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
Tried this with a "deblitzkrieged" version of what you posted and it worked, maybe it also works 4 you.
UPDATE: You are right, the upper approach would leave you with a non scrollable fragment.
I also set android:nestedScrollingEnabled="true"
on the ListView and instantly forgot about it.
If someone is still interested in a code sample, see the sample below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nested_content"
android:clipToPadding="false"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/top">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Foo Bar"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:id="@+id/bottom"
android:layout_below="@id/top">
<ListView
android:nestedScrollingEnabled="true"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scores">
</ListView>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
来源:https://stackoverflow.com/questions/56818541/listview-only-one-element-shown-with-height-wrap-content