jorgehcb
6/8/2015 - 3:57 PM

[Android] Exemplo de ViewFlipper

Exemplo de ViewFlipper

O exemplo mostra o funcionamento do ViewFlipper trocando as telas firstLayout, secondLayout e thirdLayout.

1) Criar a view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:background="#fffff0d1"
        android:gravity="center" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="490dp"
        android:layout_gravity="center_horizontal">

        <ViewFlipper
            android:id="@+id/myViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="View 1"
                    android:id="@+id/textView1"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/secondLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="View 2"
                    android:id="@+id/textView2"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/thirdLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="View 3"
                    android:id="@+id/textView3"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center" />
            </LinearLayout>
        </ViewFlipper>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:id="@+id/button"
            android:onClick="onButton1Pressed" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
            android:id="@+id/button2"
            android:onClick="onButton2Pressed" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"
            android:id="@+id/button3"
            android:onClick="onButton3Pressed" />
    </LinearLayout>

</LinearLayout>


2) Implementar o evento do click para os botões:

    public void onButton1Pressed(View view) {
        ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
        viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.firstLayout)));
        return;
    }

    public void onButton2Pressed(View view) {
        ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
        viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)));
        return;
    }

    public void onButton3Pressed(View view) {
        ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
        viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.thirdLayout)));
        return;
    }