How to get a non scrollable ListView?
Solution 1:
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 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 modification 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.
Solution 2:
The correct answer is here.
Just assign this listener to your ListView
:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you and will not be forwarded further.
}
return false;
}
});