List of usage examples for android.widget GridView getVerticalSpacing
public int getVerticalSpacing()
From source file:Main.java
public static void getListViewSize(GridView myListView) { ListAdapter myListAdapter = myListView.getAdapter(); if (myListAdapter == null) { //do nothing return null return;/*from w ww . j a va 2 s . c o m*/ } //set listAdapter in loop for getting final size int totalHeight = 0; int listval = myListAdapter.getCount(); if (listval % 3 == 0) { listval = myListAdapter.getCount() / 3; } else { listval = myListAdapter.getCount() / 3 + 1; } for (int size = 0; size < listval; size++) { View listItem = myListAdapter.getView(size, null, myListView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } System.out.println("Height " + totalHeight + " " + myListAdapter.getCount()); //setting listview item in adapter ViewGroup.LayoutParams params = myListView.getLayoutParams(); params.height = totalHeight + ((myListView.getVerticalSpacing() / 3) * (myListAdapter.getCount() - 1)); myListView.setLayoutParams(params); // print height of adapter on log Log.i("height of listItem:", String.valueOf(totalHeight)); }
From source file:util.Utils.java
/** * gridview//from w ww.j a v a 2 s . c o m * * @param listView */ public static void setListViewHeightBasedOnChildren(GridView listView) { // ?listviewadapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } // int col = listView.getNumColumns(); int totalHeight = 0; // i?4listAdapter.getCount()?4 item // listAdapter.getCount()?8 for (int i = 0; i < listAdapter.getCount(); i += col) { // ?listview?item View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // ?item totalHeight += listItem.getMeasuredHeight(); } totalHeight += listView.getVerticalSpacing(); // ?listview? ViewGroup.LayoutParams params = listView.getLayoutParams(); // params.height = totalHeight; // margin // ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // ? listView.setLayoutParams(params); }