Friday, 28 August 2015

Setting fixed height for Listview

Setting fixed height for Listview:

 

 In this post we have learn about to setting a fixed height for List view (i.e) List view having 20 list items. But displaying only 4 items. once we delete the item, the next item has to displayed.


Code:

 public  void SetCustomHeightForListView(ListView listView,int size) {
ListAdapter list_adapter = listView.getAdapter();
 ViewGroup.LayoutParams layoutParams =   listView.getLayoutParams();
int UNBOUNDED = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int grossElementHeight = 0;
        for (int g = 0; g <size; g++) {
            View childView = list_adapter.getView(g, null, listView);
            childView.measure(UNBOUNDED, UNBOUNDED);
            grossElementHeight += childView.getMeasuredHeight();
        }
        layoutParams.height = grossElementHeight;
        listView.setLayoutParams(layoutParams);
        listView.invalidate();
    }

 

To get full code

Click Here

 

 OutPut:

Before Deleting (Shows First Four Items-  fixed height for List view )

 

 

 

After Deleting (Shows Four Items- fixed height for Listview )

 

 

No comments:

Post a Comment