I need to access the methods of a RecyclerView
that is created using a third party library. What I'm doing is to get hold of the RecyclerView
View using the findViewById:
ViewGroup mainView = mainActivity.getWindow().getDecorView().findViewById(android.R.id.content);
if(feedView == null){
Object okStreamRecyclerView = mainView.findViewById(NEWSFEED_RECYCLER_ID);
feedView = (okStreamRecyclerView.getClass().getSuperclass()).getSuperclass();
scrollToTop(feedView);
}
At this point feedView.getSimpleName
results in RecyclerView
. Then I'm trying to scroll this RecyclerView
to the top using:
private void scrollToTop(Class feedView) {
log("THE VIEW IS OF TYPE: "+feedView.getName()); // androidx.recyclerview.widget.RecyclerView
try {
Method method = feedView.getMethod("getLayoutManager");
method.setAccessible(true);
LinearLayoutManager layoutManager = (LinearLayoutManager) method.invoke(feedView);
layoutManager.scrollToPositionWithOffset(0, 0);
} catch (NoSuchMethodException e) {
log("NO SUCH METHOD EXCEPTION",e);
} catch (IllegalAccessException e) {
log("ILLEGAL ACCESS EXCEPTION",e);
} catch (InvocationTargetException e) {
log("INVOCATION TARGET EXCEPTION",e);
}
}
but for some reason I get the error bellow:
IllegalArgumentException: Expected receiver of type androidx.recyclerview.widget.RecyclerView, but got java.lang.Class<androidx.recyclerview.widget.RecyclerView>
How can I convert the Class to the expected RecyclerView
? Is there any other way to get hold of this view ?
Aucun commentaire:
Enregistrer un commentaire