question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reset carousel scrolling position

See original GitHub issue

Hello! Thanks for the great library!

I have a nested custom Carousel and I need to reset it to the initial position after refreshing the content. I’m using the version 2.19.0 and I load the models in the buildModels() like this

new OfferCarouselModel_()
                .id("offer")
                .padding(carouselPadding)
                .numViewsToShowOnScreen(1.1F)
                .models(generatePopularOfferModels())
                .addIf(!offersList.isEmpty(), this);

and when I need to refresh the content I clear the list in the controller, set the new data and then requestModelBuild().

Everything works fine, but if I scrolled through the carousel before refreshing its content it’ll be at the old position, not at the first one. The Carousel class it’s really simple

@ModelView(saveViewState = true, autoLayout = Size.MATCH_WIDTH_WRAP_HEIGHT)
public class OfferCarousel extends Carousel {
    private static final int SPAN_COUNT = 1;
    private static SnapHelperFactory offerCarouselSnapHelper =
            new SnapHelperFactory() {
                @Override
                @NonNull
                public SnapHelper buildSnapHelper(Context context) {
                    return new PagerSnapHelper();
                }
            };

    public OfferCarousel(Context context) {
    super(context);
    }

    @NonNull
    @Override
    protected LayoutManager createLayoutManager() {
        return new GridLayoutManager(getContext(), SPAN_COUNT, LinearLayoutManager.HORIZONTAL, false);
    }

    @Override
    protected SnapHelperFactory getSnapHelperFactory() {
        return offerCarouselSnapHelper;
    }

}

Am I missing something? Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
elihartcommented, Feb 7, 2019

try posting the call to view.scrollToPosition(0); - when you update a carousel’s contents the model building is posted to the next frame, just like a normal EpoxyController requestModelBuild call

0reactions
AgneseBussonecommented, Feb 20, 2019

I tried different things, but the only one that seems to work in all my use cases is the following

        if (resetOfferCarouselPosition) {
            BoundViewHolders boundViewHolders = getAdapter().getBoundViewHolders();
            EpoxyModel<?> model = getAdapter().getModelById(offerCarouselId);
            if (model != null) {
                EpoxyViewHolder holder = boundViewHolders.getHolderForModel(model);
                if (holder != null) {
                    ((OfferCarousel) holder.itemView).scrollToPosition(0);
                    resetOfferCarouselPosition = false;
                }
            }
        }

at the end of the buildModels(). Is there a better solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

jCarousel resetting scroll position - Stack Overflow
You can either reset the carousel as the new data is loaded: success:function(data){ carousel.reset(); $('#thumbs').html(data); }.
Read more >
Restore RecyclerView scroll position | by Florina Muntenescu
There are several ways to ensure a correct scroll position that you might have adopted. The best one is making sure that you...
Read more >
Solved: How to reset vertical position of a canvas compone...
How to reset vertical position of a canvas component · 1)set the scroll screen's OnVisible: UpdateContext({resetCan:false}) · 2)set the scroll ...
Read more >
Bug - Scroll position resets when using slider dragger in 2022.1
To reproduce: 1. Place any input such as a slider, an input field or a button inside a scroll view. 2. Run the...
Read more >
Carousel - Bootstrap
The .active class needs to be added to one of the slides otherwise the carousel will not be visible. Also be sure to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found