Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Changing LayoutManager causing a ClassCastException #97

Open
atabouraya opened this issue Jun 10, 2016 · 1 comment
Open

Changing LayoutManager causing a ClassCastException #97

atabouraya opened this issue Jun 10, 2016 · 1 comment

Comments

@atabouraya
Copy link

atabouraya commented Jun 10, 2016

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

@apalala-dev
Copy link

apalala-dev commented Nov 15, 2016

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/)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants