AutoSwitcher

Additional

Language
Java
Version
v1.2 (Nov 26, 2018)
Created
Jul 14, 2018
Updated
Sep 3, 2021 (Retired)
Owner
shenxl (Marksss)
Contributors
dazza5000
shenxl (Marksss)
Vivek Kuvadiya (vivekkuvadiya)
3
Activity
Badge
Generate
Download
Source code

English | 中文文档

AndroidAutoSwitcher

AutoSwitchView is a view that can automatically switch between two children (items). Compared to ViewFlipper, it has better stability for reusing its children when working on large data sets. Compared to AdapterViewFlipper, its expansibility is more excellent.

Usage

Add the JitPack repository to your build file

allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
 }

Add the dependency

implementation 'com.github.Marksss:AndroidAutoSwitcher:v1.2'

Code in XML

    <com.switcher.AutoSwitchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:switcher_repeatCount="1"
        app:switcher_autoStart="true"/>

If you want the switching animation to be infinite, you just need to set switcher_repeatCount -1.

Code in Activity

AutoSwitchView as = (AutoSwitchView) findViewById(R.id.yourid);
as.setAdapter(new YourAdapter());
as.setSwitchStrategy(new YourStrategy()); // See Switching Strategy
as.startSwitcher(); // If you have set autoStart true, this is not needed.

Switching Strategy

You can easily customize swtiching animations you like with SwitchStrategy. It supports both Animation and ObjectAnimator. Here are some builders of SwitchStrategy I have offered as follows.

  • AnimationStrategyBuilder: customize your own animation with Animation;
  • AnimatorStrategyBuilder: customize your own animation with ObjectAnimator;
  • CarouselStrategyBuilder: seamlessly switch between two items in different directions;
  • ContinuousStrategyBuilder: switch between items smoothly without any pauses;

An example:

as.setSwitchStrategy(
     new AnimationStrategyBuilder(this, R.anim.anim_in, R.anim.anim_out).
         build()
);

Another example:

as.setSwitchStrategy(
     new CarouselStrategyBuilder().
         setAnimDuration(900).
         setInterpolator(new AccelerateDecelerateInterpolator()).
         setMode(DirectionMode.right2Left).
         build()
);

In most cases, strategies above are enough. If you want to customize animation that is not so complicated, you can use AnimationStrategyBuilde or AnimatorStrategyBuilder with your own Animation or ObjectAnimator.

Additional

It usually is unnecessary. But if you want to customize more complicated animations, then you neeed to create your own SwitchStrategy through adding SingleOperator into BaseBuilder (init->next->withEnd) in turn to control all movements of the switcher.

new SwitchStrategy.BaseBuilder().
    init(new SingleOperator() {
        @Override
        public void operate(AutoSwitchView switcher, ChainOperator operator) {
            ...//init your animation
        }
    }).next(new SingleOperator() {
        @Override
        public void operate(AutoSwitchView switcher, ChainOperator operator) {
            ...//run your own animation
        }
    }).withEnd(new SingleOperator() {
        @Override
        public void operate(AutoSwitchView switcher, ChainOperator operator) {
            ...//cancle or end your animation
        }
    }).build();

License

AutoSwitchView is released under the Apache License Version 2.0.