Created
September 30, 2015 13:25
-
-
Save jefferson/9f117c5990abcae9ec2a to your computer and use it in GitHub Desktop.
Android ListView - Calc the current height from 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
package Util; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListAdapter; | |
import android.widget.ListView; | |
/** | |
* Created by JeffersonAlves on 28/09/2015. | |
*/ | |
public class Util { | |
public static class Tools{ | |
public static void calculeHeightListView(ListView listView) { | |
int totalHeight = 0; | |
ListAdapter adapter = listView.getAdapter(); | |
int lenght = adapter.getCount(); | |
listView.requestLayout(); | |
for (int i = 0; i < lenght; i++) { | |
View listItem = adapter.getView(i, null, listView); | |
listItem.measure(0, 0); | |
totalHeight += listItem.getMeasuredHeightAndState(); | |
} | |
ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
params.height = totalHeight | |
+ (listView.getDividerHeight() * (adapter.getCount() - 1)) + 10; | |
listView.setLayoutParams(params); | |
listView.requestLayout(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment