Hi guys, I got my answer using Gridview as the title of the list.
I just calculated the height of the separator for the Gridview using the method below and it worked perfectly as I wanted.
public static void setDynamicHeightGridView(GridView mListView,String oddeven) { ListAdapter mListAdapter = mListView.getAdapter(); if (mListAdapter == null) { return; } int height = 0; int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED); for(int i = 0; i < mListAdapter.getCount(); i++){ View listItem = mListAdapter.getView(i, null, mListView); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); height += listItem.getMeasuredHeight(); itemHeight=listItem.getMeasuredHeight()/3; } ViewGroup.LayoutParams params = mListView.getLayoutParams(); if(oddeven.equals("odd")){ if(mListAdapter.getCount()>=5){ int count=((mListAdapter.getCount()-5)/2) + 1; params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5); }else{ params.height = height - (height / 3) + 20; } }else if(oddeven.equals("even")) { params.height = height/2 + 20; } mListView.setLayoutParams(params); mListView.requestLayout(); }
Edited by:
Finally, the Exact Answer:
For an even number of views:
params.height = height/2 + 20;
For an odd number of views:
params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);
Where:
I used 5 as a number to compare bcoz after this adapter value. Count the change in space between the gridview and listview increases with fixed values.
For spaces up to 5, the else part is processed.
itemHeight is an incremental value with which space is increased
20 is a space field between gridview and list
(count x 5) - this is the value for managing margin as the elements increase.
bcoz this gave me double the space for the height of the Gridview above the Listview for an even number of views
and gridview + half height space for an odd number of views
Hope this helps :)
source share