Skip to content

Instantly share code, notes, and snippets.

@HViktorTsoi
Created October 18, 2018 14:11
Show Gist options
  • Save HViktorTsoi/c92eb6c9cc2930534df0775a2d4b7924 to your computer and use it in GitHub Desktop.
Save HViktorTsoi/c92eb6c9cc2930534df0775a2d4b7924 to your computer and use it in GitHub Desktop.
解决Android的ScrollVIew嵌套一个ListView之后多个条目ListView显示不全的问题
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:background="#EDEDED">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical">
<ListView
android:id="@+id/lst_video_item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:divider="#B8B8B8" >
</ListView>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
/**
* scrollview嵌套listview显示不全解决
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment