Changing LayoutManager causing a ClassCastException
atabouraya opened this issue · 1 comments
I was trying to change the LayoutManager from GridLayoutManager to LinearLayoutManager, It works fine until you scroll to the last item. According to the StackTrace this method is what's causing the Issue.
` private int getLastVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
int lastVisibleItemPosition = -1;
if (layoutManagerType == null) {
if (layoutManager instanceof GridLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
} else if (layoutManager instanceof LinearLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
} else {
throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
}
}
switch (layoutManagerType) {
case LINEAR:
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case GRID:
lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case STAGGERED_GRID:
lastVisibleItemPosition = caseStaggeredGrid(layoutManager);
break;
}
return lastVisibleItemPosition;
}`
It looks like layoutManagerType is only set when it's null, which means the first time only, that means it remains as GridLayoutManager when I set the LayoutManager the second time to LinearLayoutManager
Any news on this issue?
It would be nice to have the bloc :
if (layoutManagerType == null) { if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.GRID; } else if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; } else { throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } }
in the method setLayoutManager() instead...
I have done a fork in which I made the change, I am a beginner at using GitHub (and git), but if you're interested it's there under the tag v1.1.5 :
https://github.com/Kobatsu/SuperRecyclerView
It can be imported easily in Android Studio using JitPack (https://jitpack.io/)