I've used the TouchInterceptor class in the Google Music application in my application. This class allows you to drag and drop list items into different positions in the list.
The TouchInterceptor class makes a call to a method called smoothScrollBy. This method is only available in API 8+.
I want to target my application at API 7+, so I need to use reflection to execute smoothScrollBy only if it exists.
In the constructor for TouchInterceptor, I've added the following:
Method[] ms = this.getClass().getDeclaredMethods();
if (ms != null) {
for (Method m : ms) {
if (m != null && m.toGenericString().equals("smoothScrollBy")) {
Class[] parameters = m.getParameterTypes();
if (parameters != null && parameters.length == 1 && parameters[0].getName().equals("int")) {
mSmoothScrollBy = m;
}
}
}
}
This should find the smoothScrollBy method and assign it to a new member variable of TouchInterceptor called mSmoothScrollBy (Method).
I'm debugging through on an Android 2.2 (API 8) emulator and unfortunately the method is never found. My guess is getDeclaredMethods() does not return it in the array because smoothScrollBy is a method of AbsListView, which is inherited by ListView and ultimately TouchInterceptor.
I've tried casting this to AbsListView before calling getClass().getDeclaredMethods() with no success.
How can I properly get ahold of smoothScrollBy so I can invoke it when available?
Update:
I've also tried the following to no avail:
Method test = null;
try {
test = this.getClass().getMethod("smoothScrollBy", new Class[] { Integer.class });
}
catch (NoSuchMethodException e) {
}