Android Application Development BASICS (11) -- viewflipper

Source: Internet
Author: User

I. Overview

Viewflipper is used to display multiple pages. The display between multiple pages is generally implemented by sliding gestures. For example, when you are on the home page, you can slide to display another page, it is a bit like switching between activities. The following implementation does not involve the gesture recognition function.


Ii. Requirements

Master the use of viewflipper.


Iii. Implementation

Create a project myflipper, modify the/RES/layout/Main. xml file, and add a viewflipper file. The complete main. xml file is as follows:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <ViewFlipper
8 android:id="@+id/viewflipper"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 >
12
13 </ViewFlipper>
14
15 </LinearLayout>

Create two new files firstview. xml and secondview. xml under/RES/layout. The content of these two files is almost the same, but the content displayed is different. firstview. XML is as follows:

 

1 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
2 Android: layout_width = "fill_parent"
3 Android: layout_height = "fill_parent"
4 Android: Orientation = "vertical">
5
6 <textview
7 Android: layout_width = "fill_parent"
8 Android: layout_height = "wrap_content"
9 Android: text = "This is the first view"
10 Android: textcolor = "# ffff0000"
11 Android: gravity = "center_horizontal"
12 Android: textsize = "20dip"
13/>
14
15
16 <button
17 Android: Id = "@ + ID/firstbutton"
18 Android: layout_width = "fill_parent"
19 Android: layout_height = "wrap_content"
20 Android: text = "display the second view"
21/>
22
23
24 </linearlayout>

Secondview. XML is as follows:

 

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6
7 <textview
8 Android: layout_width = "fill_parent"
9 Android: layout_height = "wrap_content"
10 Android: text = "this is the second view"
11 Android: textcolor = "# ff0000ff"
12 Android: gravity = "center_horizontal"
13 Android: textsize = "20dip"
14/>
15
16 <button
17 Android: Id = "@ + ID/secondbutton"
18 Android: layout_width = "fill_parent"
19 Android: layout_height = "wrap_content"
20 Android: text = "display the first view"
21/>
22
23
24 </linearlayout>

Next, modify the myflipperactivity. Java file to define a viewflipper object, add two views to it, set the listening of two buttons, and display the front and back views in the listener.

 

1 package com. Nan. Flipper;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 import Android. View. layoutinflater;
6 Import Android. View. view;
7 Import Android. widget. Button;
8 Import Android. widget. viewflipper;
9
10 public class myflipperactivity extends Activity
11 {
12 private viewflipper mviewflipper = NULL;
13 private layoutinflater mlayoutinflater = NULL;
14
15 private button firstbutton = NULL;
16 private button secondbutton = NULL;
17
18/** called when the activity is first created .*/
19 @ override
20 public void oncreate (bundle savedinstancestate)
21 {
22 super. oncreate (savedinstancestate );
23 setcontentview (R. layout. Main );
24
25 mviewflipper = (viewflipper) This. findviewbyid (R. Id. viewflipper );
26
27 mlayoutinflater = layoutinflater. From (myflipperactivity. This );
28 // change layout file firstview. XML to view object
29 view firstview = mlayoutinflater. Inflate (R. layout. firstview, null );
30 // change the layout file secondview. XML to a view object
31 view secondview = mlayoutinflater. Inflate (R. layout. secondview, null );
32 // Add View
33 mviewflipper. addview (firstview );
34 // Add View
35 mviewflipper. addview (secondview );
36
37 firstbutton = (button) firstview. findviewbyid (R. Id. firstbutton );
38 // set button listening
39 firstbutton. setonclicklistener (New View. onclicklistener ()
40 {
41
42 @ override
43 public void onclick (view V)
44 {
45 // todo auto-generated method stub
46 // display the last view
47 mviewflipper. shownext ();
48}
49 });
50
51 secondbutton = (button) secondview. findviewbyid (R. Id. secondbutton );
52 // set button listening
53 secondbutton. setonclicklistener (New View. onclicklistener ()
54 {
55
56 @ override
57 public void onclick (view V)
58 {
59 // todo auto-generated method stub
60 // display the previous view
61 mviewflipper. showprevious ();
62}
63 });
64
65}
66
67}

Okay, run the program:

 

Click "display second view". The effect is as follows:

 

All right, complete.
If Gesture Recognition is added, you can slide to switch the page.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.