Created
October 18, 2018 14:11
-
-
Save HViktorTsoi/c92eb6c9cc2930534df0775a2d4b7924 to your computer and use it in GitHub Desktop.
解决Android的ScrollVIew嵌套一个ListView之后多个条目ListView显示不全的问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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