ViewFlipper inherits from frame layout, so it displays a single view at a time.
consider this layout:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Flip"
- android:id="@+id/btn"
- android:onClick="ClickHandler"/>
- <ViewFlipper
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/flip">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Item1"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Item2"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Item3"/>
- </ViewFlipper>
- </LinearLayout>
Just a ViewFlipper container that contains three text views
Now we want to flip the views when the button is clicked.
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- flip=(ViewFlipper)findViewById(R.id.flip);
- }
- {
- flip.showNext();
- }
If we want to flip in reverese direction we could use flip.showPrevious() instead. If you want to flip to a specific view do the following: flip.setDisplayedChild(indexOfView) replace indexOfView with the integer representing the index of the view you want to switch to.
We can add animations to the child views when they appear or disappear:
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- flip=(ViewFlipper)findViewById(R.id.flip);
- //when a view is displayed
- flip.setInAnimation(this,android.R.anim.fade_in);
- //when a view disappears
- flip.setOutAnimation(this, android.R.anim.fade_out);
- }
We can also set the ViewFlipper to flip views automatically when the button is clicked:
- {
- //specify flipping interval
- flip.setFlipInterval(1000);
- flip.startFlipping();
- }
We can stop the flipping by calling flip.stopFlipping(); method or we can set the flipper to flip autommatically when the activity starts by changing our code.
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- flip=(ViewFlipper)findViewById(R.id.flip);
- flip.setInAnimation(this,android.R.anim.fade_in);
- flip.setOutAnimation(this, android.R.anim.fade_out);
- flip.setFlipInterval(1000);
- flip.setAutoStart(true);
- }
More about this control you can read by this url: http://developer.android.com/reference/android/widget/ViewFlipper.html
No comments:
Post a Comment