Search code examples
androidviewflipper

viewflipper flipping


I have a viewflipper and two buttons "next" and "previous" outside the viewflipper. I want viewflipper to show next background on clicking "next" and previous background on clicking "previous". How can I do that?


Solution

  • There are two techniques to achieve your requirements.

    1. Use showPrevious and showNext methods of ViewFlipper class. One thing you have to know about these methods is by calling any method continuously, it will start displaying it's children in ascending order for showNext and descending order for showPrevious.

    Example : View flipper has 4 children say 0, 1, 2, 3. Initially it will display first item i.e., child 0. Now if you called showNext 6 times continuously, it starts displaying 1, 2, 3, 0, 1, 2. So finally it will display child 2. Same procedure in descending order for shoePrevious also.

    2. Use setDisplayedChild method to display a particular child included in the ViewFlipper.

    Example: The view flipper may as below in XML layout file

    <ViewFlipper android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <include android:id="@+id/first"  layout="@layout/first_view" />
        <include android:id="@+id/second"  layout="@layout/second_view" />
    </ViewFlipper>
    

    To display first child you can use setDisplayedChild in two ways.

    call setDisplayedChild(R.id.first); or setDisplayedChild(0); means you can use Id of child in ViewFlipper or position of child in ViewFlipper.

    So depending on actual requirements, decide which method is appropriate.

    I hope you understand this.