Last active
March 5, 2018 02:24
-
-
Save booknara/7091616 to your computer and use it in GitHub Desktop.
ListView Adjustment(Height) in ScrollView
This file contains 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
package com.daeheehan.util; | |
import android.view.View; | |
import android.view.View.MeasureSpec; | |
import android.view.ViewGroup; | |
import android.widget.ListAdapter; | |
import android.widget.ListView; | |
/** | |
* List View Utility | |
* Reference : http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html | |
* | |
* @author Daehee Han ([email protected]) | |
* @since 10/17/2013 | |
* @version 1.0.0 | |
* | |
*/ | |
public class ListViewUtil { | |
private static final String CNAME = ListViewUtil.class.getSimpleName(); | |
public static void setListViewHeightBasedOnChildren(ListView listView) { | |
ListAdapter listAdapter = listView.getAdapter(); | |
if (listAdapter == null) { | |
// pre-condition | |
return; | |
} | |
int totalHeight = 0; | |
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); | |
for (int i = 0; i < listAdapter.getCount(); i++) { | |
View listItem = listAdapter.getView(i, null, listView); | |
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); | |
totalHeight += listItem.getMeasuredHeight(); | |
} | |
ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); | |
listView.setLayoutParams(params); | |
listView.requestLayout(); | |
} | |
} |
Nice code !
this doesn't give the right height ..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, it works very good