Skip to content

Instantly share code, notes, and snippets.

@jefferson
Created September 30, 2015 13:25
Show Gist options
  • Save jefferson/9f117c5990abcae9ec2a to your computer and use it in GitHub Desktop.
Save jefferson/9f117c5990abcae9ec2a to your computer and use it in GitHub Desktop.
Android ListView - Calc the current height from ListView.
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