epoxy icon indicating copy to clipboard operation
epoxy copied to clipboard

Carousel as a sticky header

Open mehdi-salehi opened this issue 4 years ago • 1 comments
trafficstars

Hi When I trying to put the Carousel in a sticky header by StickyHeaderLinearLayoutManager, scroll position of the Carousel is not saving in states and after scrolling down, it's back to a diffrent position;

        recyclerView.setController(controller);
        recyclerView.setLayoutManager(new StickyHeaderLinearLayoutManager(this));
        controller.requestModelBuild();
public class HeaderViewController extends EpoxyController implements StickyHeaderCallbacks {
    @Override
    protected void buildModels() {
        for (int i = 1; i < 5; i++) {
            new MainModel_().title("test " + i).id(i).addTo(this);
        }

        new HeaderModel_().title("header").id(100).addTo(this);

        for (int i = 10; i < 30; i++) {
            new MainModel_().title("test " + i).id(i).addTo(this);
        }

        List<ImageModel_> photoModels = new ArrayList();
        for (int i = 105; i < 110; i++) {
            photoModels.add(new ImageModel_()
                    .id(i));
        }

        new SimpleCarouselModel_()
                .id("carousel")
                .models(photoModels)
                .paddingDp(0)
                .addTo(this);

        new HeaderModel_().title("header 2").id(200).addTo(this);

        for (int i = 40; i < 60; i++) {
            new MainModel_().title("test " + i).id(i).addTo(this);
        }
    }

    @Override
    public boolean isStickyHeader(int position) {
        return getAdapter().getModelAtPosition(position) instanceof SimpleCarouselModel_;
    }

    @Override
    public void setupStickyHeaderView(@NotNull View stickyHeader) {
        SimpleCarousel carousel = (SimpleCarousel) stickyHeader;
        super.setupStickyHeaderView(carousel);
    }

    @Override
    public void teardownStickyHeaderView(@NotNull View stickyHeader) {
        SimpleCarousel carousel = (SimpleCarousel) stickyHeader;
        super.teardownStickyHeaderView(carousel);
    }
}
@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT, saveViewState = true)
public class SimpleCarousel extends Carousel {
    private final Context context;

    public SimpleCarousel(Context context) {
        super(context);
        this.context = context;

//        setDefaultGlobalSnapHelperFactory(null);
        setDefaultItemSpacingDp(0);
        setPaddingDp(0);
    }


    @Override
    protected LayoutManager createLayoutManager() {
        return new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false);
    }

}

mehdi-salehi avatar May 11 '21 11:05 mehdi-salehi

I have the same issue, too. Have you found any solution?

DawnNguyenAhiho avatar Apr 11 '24 10:04 DawnNguyenAhiho