Android Learning Note (two or three): Use of multi-page display-flipper

Source: Internet
Author: User

In addition to the tab display of multi-page content in an activity, you can also use Flipper,flipper without a label, which is the way a page is displayed.

Example one: Flipper of the Foundation

1) Android XML file

Flipper is defined by the Viewflipper, which in turn puts the contents of each page.

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout. >
<button android:id= "@+id/c94_flip_me" ... android:text= "Flip Me"/>
<ViewflipperAndroid:id= "@+id/c94_details"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<textview android:layout_width= "Fill_parent" <!--the first element in Viewflipper--
android:layout_height= "Wrap_content"
Android:textstyle= "Bold"
Android:textcolor= "#FF00FF00"
Android:text= "This is the first panel"/>
<textview. android:text= "This is the Second panel"/> <!--viewflipper second element--
<textview. android:text= "This is the third panel"/> <!--viewflipper in the third element--
</ViewFlipper>
</LinearLayout>

2) Code Writing

We click on the button with the ID c94_details, then display the elements in the viewflipper in turn, looping through the display as shown:

public class Chapter9test4 extends activity{
PrivateViewflipperFlipper = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.CHAPTER_9_TEST4);

Flipper= (viewflipper) Findviewbyid (r.id.c94_details);
Button button = (button) Findviewbyid (R.id.c94_flip_me);
Button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View arg0) {
Each time a button is clicked, the display in Viewflipper is replaced with the next element, and if it is the last element, start from the beginning
Flipper.shownext ();
}
});
}
}

Example two: Adding Flipper elements and auto-paging

The XML file in this example is as follows, there is only viewflipper in LinearLayout, and there is no element set in Viewflipper. Very simple, no longer show, the following is the source code;

public class Chapter9test5 extends activity{
Private Viewflipper flipper = null;
public static string[] items={"Lorem", "Ipsum", "dolor", "sit", ...//several String

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.CHAPTER_9_TEST5);
Step 1: Get an instance of Flipper
Flipper = (viewflipper) Findviewbyid (r.id.c95_details);
Step 2: Set the dynamic effect of the Flipper page, described in the following, here gives the two effects of entering and leaving
Flipper.setinanimation(Animationutils.loadanimation (this,r.anim.push_left_in));
Flipper.setoutanimation(Animationutils.loadanimation (this,r.anim.push_left_out));
Step 3: Dynamically add the view of each page via Flipper.addview to Flipper
for (String Item:Chapter9Test5.items) {
Button button = New button (this);
Button.settext (item);
Flipper.AddView(Button, new Viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
}
Step 4: Set the time interval for auto-paging, this example is 3 seconds, or it can be set through Android:interval in the XML file
Flipper.Setflipinterval(3000);
Step 5: Start auto-paging and stop by Stopflipping ().
Flipper.startflipping();
}
}

Here's a more troublesome flip-up of the dynamic effect, that is, step 2. We created the letter under the res/Floder, named Anim, which will be stored to describe the dynamic XML file, we can byte using the SDK automatic example, in the Anim by mail import->general->filesystem-> Next-> in Browser .../android-sdk-linux_x86/samples/android-9/apidemos/res/anim,demo example has given some examples, we choose Push_ Left_in and push_left_out can be imported.

Let's look at the Push_left_in.xml file, which describes the dynamic way:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate android:fromxdelta= "100%p" android:toxdelta= "0" android:duration= "/>"
<alpha android:fromalpha= "0.0" android:toalpha= "1.0" android:duration= "/>"
</set>

Let's look at the Push_left_out.xml file, which describes the dynamic way:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate android:fromxdelta= "0" android:toxdelta= " -100%p" "android:duration="/>
<alpha android:fromalpha= "1.0" android:toalpha= "0.0" android:duration= "/>"
</set>

RELATED links: My Andriod development related articles

Android Learning Note (two or three): Use of multi-page display-flipper

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.