Android AdapterViewFlipper With Examples

In this tutorial, we’ll be discussing and implementing the AdapterViewFlipper class in our Android Application.

Android AdapterViewFlipper

AdapterViewFlipper class is a subclass of the ViewAnimator class and is used to flip between 2 or more views such that only one view is displayed at a time. This is commonly used in slides.

Difference between ViewFlipper and AdapterViewFlipper

ViewFlipper is used to display all the slide views initially. Statically. That means the views won’t be recycled.

AdapterViewFlipper is used to display all the child views using the Adapter. Similar to a ListView/Spinner/RecyclerView. So there is space for recycling the views and also loading views dynamically.

An AdapterViewFlipper is defined in the XML layout in the following manner:

Following are the important XML attributes used:

  • android:autoStart – To start the view flipping once the adapter is loaded.
  • android:flipInterval – Used to set the time interval after which the next child view is presented. Set in milliseconds.

Each of these has their Java counterpart methods as well.

Following are some of the other Java methods used on AdapterViewFlipper class:

  • isFlipping() – Returns a boolean to indicate whether the AdapterViewFlipper flipping is ongoing or not.
  • showNext() – To manually go to the next view.
  • showPrevious() – Goes to the previous view.
  • startFlipping() – To begin flipping. This gets automatically called if autostart is true.
  • stopFlipping() – To stop flipping.
  • isAutoStart() – Returns true if startFlipping was automatically called when the child views where attached to the Adapter.

In the following section, we’ll be creating an android application using the above discussed AdapterViewFlipper concepts.

Project Structure

androidly-listview-project-structure (1)

Code

The code for the activity_main.xml layout is given below:

The code for the row_view_item.xml is given below:

The code for the MainActivity.java class is given below:

We’ve defined a Model class AndroidVersion.
Data is populated in the Adapter class from an ArrayList of AndroidVersion.
In order to save memory, we’ll stop the flipping when the activity is in the background.

We can add animations when a view enters and exits the AdapterViewFlipper by:

adapterViewFlipper.setInAnimation() and adapterViewFlipper.setInAnimation()

The code for the AdapterViewFlipperAdapter.java class is given below:

The output of the above application in action is given below:

android adapter view flipper output

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply