Struct
Struct

Reputation: 970

How to stop infinite loop on sliders

I'm using this Slider.

This is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDemoSlider = (SliderLayout)findViewById(R.id.slider);

        HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
        file_maps.put("Hannibal",R.drawable.hannibal);
        file_maps.put("Big Bang Theory",R.drawable.bigbang);
        file_maps.put("House of Cards",R.drawable.house);
        file_maps.put("Game of Thrones", R.drawable.game_of_thrones);

        for(String name : file_maps.keySet()){
            TextSliderView textSliderView = new TextSliderView(this);
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(file_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);

            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                        .putString("extra",name);

           mDemoSlider.addSlider(textSliderView);
        }
        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.stopAutoCycle();
        mDemoSlider.addOnPageChangeListener(this);
    }

Is there any way to stop infinite loop on sliders. I read that I should add mDemoSlider.stopAutoCycle(); but this does not have any effect.

Upvotes: 1

Views: 919

Answers (1)

Mauker
Mauker

Reputation: 11497

Sorry, but you can't. At least not with this lib.

I've looked into the code and I saw that this "infinite scrolling" is default behavior, and if you want to disabled it you have either to implement your own slider, or suggest and edit for the original author...

The "problem" is on those two methods of the SliderLayout.java class:

/**
 * move to next slide.
 */
public void moveNextPosition(boolean smooth) {

    if (getRealAdapter() == null)
        throw new IllegalStateException("You did not set a slider adapter");

    mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1, smooth);
}

// ...

/**
 * move to prev slide.
 */
public void movePrevPosition(boolean smooth) {

    if (getRealAdapter() == null)
        throw new IllegalStateException("You did not set a slider adapter");

    mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1, smooth);
}

And the setCurrentItem is located inside the ViewPagerEx.java class, where you can see that this infinite scrolling is default behavior. (Look at the setCurrentItemInternal method).

 void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
        if (mAdapter == null || mAdapter.getCount() <= 0) {
            setScrollingCacheEnabled(false);
            return;
        }
        if (!always && mCurItem == item && mItems.size() != 0) {
            setScrollingCacheEnabled(false);
            return;
        }

        if (item < 0) {
            item = 0;
        } else if (item >= mAdapter.getCount()) {
            item = mAdapter.getCount() - 1;
        }
        final int pageLimit = mOffscreenPageLimit;
        if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {
            // We are doing a jump by more than one page.  To avoid
            // glitches, we want to keep all current pages in the view
            // until the scroll ends.
            for (int i=0; i<mItems.size(); i++) {
                mItems.get(i).scrolling = true;
            }
        }
        final boolean dispatchSelected = mCurItem != item;

        if (mFirstLayout) {
            // We don't have any idea how big we are yet and shouldn't have any pages either.
            // Just set things up and let the pending layout handle things.
            mCurItem = item;
            triggerOnPageChangeEvent(item);
            requestLayout();
        } else {
            populate(item);
            scrollToItem(item, smoothScroll, velocity, dispatchSelected);
        }
    }

Upvotes: 1

Related Questions