I need to totally disable overscroll in my listviews so I can implement my own overscroll functionality.
Seems to be simple enough when looking at the core listview classes, just by setting the overscroll
mode to OVERSCROLL_NEVER. This fine work on my Samsung Galaxy s2. But doesn't work For Galaxy Tab 2.3.3.
Has anyone had much experience with samsung ListView customizations that can help me?
It worked for me on Samsung Galaxy Tab (with Android 2.2):
try {
// list you want to disable overscroll
// replace 'R.id.services' with your list id
ListView listView = ((ListView)findViewById(R.id.services));
// find the method
Method setEnableExcessScroll =
listView.getClass().getMethod("setEnableExcessScroll", Boolean.TYPE);
// call the method with parameter set to false
setEnableExcessScroll.invoke(listView, Boolean.valueOf(false));
}
catch (SecurityException e) {}
catch (NoSuchMethodException e) {}
catch (IllegalArgumentException e) {}
catch (IllegalAccessException e) {}
catch (InvocationTargetException e) {}
You have to set the height of the listview to a fix value. If your content is dynamically there is a nice function to measure the actual listsize after resetting the adapter:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
you have to call this static method with your listview as paramter after setting up your adapter. You can now add an scrollview (inside is your listview and other views). I would say this behavior in 2.3.3 an earlier is a small bug... there is no easy way to include the listview inside a scrollview except the way I described. For that reason they introduced the OVERSCROLL_NEVER mode :)
Code is from DougW!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With