/** 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); }
/** remove all the sliders. Notice: It's a not perfect method, a very small bug still exists. */ public void removeAllSliders() { if (getRealAdapter() != null) { int count = getRealAdapter().getCount(); getRealAdapter().removeAllSliders(); // a small bug, but fixed by this trick. // bug: when remove adapter's all the sliders.some caching slider still alive. mViewPager.setCurrentItem(mViewPager.getCurrentItem() + count, false); } }
/** * set current slider * * @param position */ public void setCurrentPosition(int position, boolean smooth) { if (getRealAdapter() == null) throw new IllegalStateException("You did not set a slider adapter"); if (position >= getRealAdapter().getCount()) { throw new IllegalStateException("Item position is not exist"); } int p = mViewPager.getCurrentItem() % getRealAdapter().getCount(); int n = (position - p) + mViewPager.getCurrentItem(); mViewPager.setCurrentItem(n, smooth); }
public SliderLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; LayoutInflater.from(context).inflate(R.layout.slider_layout, this, true); final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SliderLayout, defStyle, 0); mTransformerSpan = attributes.getInteger(R.styleable.SliderLayout_pager_animation_span, 1100); mTransformerId = attributes.getInt(R.styleable.SliderLayout_pager_animation, Transformer.Default.ordinal()); mAutoCycle = attributes.getBoolean(R.styleable.SliderLayout_auto_cycle, true); int visibility = attributes.getInt(R.styleable.SliderLayout_indicator_visibility, 0); for (PagerIndicator.IndicatorVisibility v : PagerIndicator.IndicatorVisibility.values()) { if (v.ordinal() == visibility) { mIndicatorVisibility = v; break; } } mSliderAdapter = new SliderAdapter(mContext); PagerAdapter wrappedAdapter = new InfinitePagerAdapter(mSliderAdapter); mViewPager = (InfiniteViewPager) findViewById(R.id.daimajia_slider_viewpager); mViewPager.setAdapter(wrappedAdapter); mViewPager.setOnTouchListener( new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_UP: recoverCycle(); break; } return false; } }); attributes.recycle(); setPresetIndicator(PresetIndicators.Center_Bottom); setPresetTransformer(mTransformerId); setSliderTransformDuration(mTransformerSpan, null); setIndicatorVisibility(mIndicatorVisibility); if (mAutoCycle) { startAutoCycle(); } }
/** * get the current item position * * @return */ public int getCurrentPosition() { if (getRealAdapter() == null) throw new IllegalStateException("You did not set a slider adapter"); return mViewPager.getCurrentItem() % getRealAdapter().getCount(); }
private SliderAdapter getRealAdapter() { PagerAdapter adapter = mViewPager.getAdapter(); if (adapter != null) { return ((InfinitePagerAdapter) adapter).getRealAdapter(); } return null; }
private InfinitePagerAdapter getWrapperAdapter() { PagerAdapter adapter = mViewPager.getAdapter(); if (adapter != null) { return (InfinitePagerAdapter) adapter; } else { return null; } }
/** * get current slider. * * @return */ public BaseSliderView getCurrentSlider() { if (getRealAdapter() == null) throw new IllegalStateException("You did not set a slider adapter"); int count = getRealAdapter().getCount(); int realCount = mViewPager.getCurrentItem() % count; return getRealAdapter().getSliderView(realCount); }
/** * set the duration between two slider changes. * * @param period * @param interpolator */ public void setSliderTransformDuration(int period, Interpolator interpolator) { try { Field mScroller = ViewPagerEx.class.getDeclaredField("mScroller"); mScroller.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller(mViewPager.getContext(), interpolator, period); mScroller.set(mViewPager, scroller); } catch (Exception e) { } }
/** * remove the slider at the position. Notice: It's a not perfect method, a very small bug still * exists. */ public void removeSliderAt(int position) { if (getRealAdapter() != null) { getRealAdapter().removeSliderAt(position); mViewPager.setCurrentItem(mViewPager.getCurrentItem(), false); } }
/** * set ViewPager transformer. * * @param reverseDrawingOrder * @param transformer */ public void setPagerTransformer(boolean reverseDrawingOrder, BaseTransformer transformer) { mViewPagerTransformer = transformer; mViewPagerTransformer.setCustomAnimationInterface(mCustomAnimation); mViewPager.setPageTransformer(reverseDrawingOrder, mViewPagerTransformer); }
public void removeOnPageChangeListener(ViewPagerEx.OnPageChangeListener onPageChangeListener) { mViewPager.removeOnPageChangeListener(onPageChangeListener); }
public void addOnPageChangeListener(ViewPagerEx.OnPageChangeListener onPageChangeListener) { if (onPageChangeListener != null) { mViewPager.addOnPageChangeListener(onPageChangeListener); } }