I want to show a Profile screen for my users.
It must have three views (2 Buttons
and a ImageView
) and a ListView
to show the
I found a very simple solution for this. Just get the adapter of the listview and calculate its size when all items are shown. The advantage is that this solution also works inside a ScrollView.
Example:
public static void justifyListViewHeightBasedOnChildren (ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
Call this function passing over your ListView object:
justifyListViewHeightBasedOnChildren(myListview);
The function shown above is a modidication of a post in: Disable scrolling in listview
Please note to call this function after you have set the adapter to the listview. If the size of entries in the adapter has changed, you need to call this function as well.
with the following instruction:
name_lista.getLayoutParams (). height = new_size
new_size is a variable that you will calculate according to the number of elements of your list, for example:
new_size = 100 * list_size;
You can do this by
listView.setScrollContainer(false);
for more please check
How to get a non scrollable ListView?
I think the best way would be to put the 2 buttons and the image view in a LinearLayout (or any layout that suits your need) and add this layout as the list header using the addHeaderView method:
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
if you have to show limited number of items in list view and want to stop list view from scrolling then you must keep listview height greater then the items total height.
for example you want to show 3 items. (height of row is 30). then items total height becomes 3 x 30dp = 90dp,
so now you have to set listview height greater then 90. e.g: 100dp. so now your listview will not scroll in any case.
Adding them to the ListView as first Item seems like a pretty good solution.
To make the View unselectable just get the view and .setClickable(false)
.