function and usage of Viewswitcher view

Source: Internet
Author: User

Viewswitcher represents the view switch component, which itself inherits the Framelayout, so multiple views can be stacked together, showing only one component at a time. Viewswitcher supports specifying animation effects when program control switches from one view to another view.

In order to add multiple components to Viewswitcher, the viewfactory is typically set by calling the Viewswitcher Setfactory (viewswitcher.viewfactory) method, and by the Viewfactory Create a view for it.

The use of Viewfactory is described below with an example.

Example: Android system Launcher Interface

Android early version of the Launcher interface is scroll up and down, Android4.2 Launcher interface has been implemented split screen, left and right scrolling, this example is through the viewswitcher to achieve Android4.2 split screen, left and right scrolling effect.

To achieve this effect, the program main interface considers using Viewswitcher to combine multiple gridview, each of which represents a screen application, and each cell in the GridView displays an application's icon and program name.

The main interface layout of the program is as follows:

Program list:/viewswitcherdemo/res/layout/activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "> <!-- Define a Viewswitcher component--<viewswitcher android:id= "@+id/viewswitcher" android:layout_width= "fill_p    Arent "android:layout_height=" fill_parent "></ViewSwitcher> <!--define a button that scrolls to the previous screen-- <button android:id= "@+id/button_prev" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_alignparentbottom=" true "android:layout_alignparentleft=" true "Android:o nclick= "prev" android:text= "<"/> <!--define a button to scroll to the next screen--<button Android:id = "@+id/button_next" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Layout_alignparentbottom= "true" AndroiD:layout_alignparentright= "true" android:onclick= "Next" android:text= ">"/></relativelayout& Gt
The layout file defines two buttons that control the list of programs on the previous and next screens of the viewswitcher. The use of buttons in the program to control the page, you can also modify the changes to a sliding scrolling screen.

The program will consider using extended baseadapter to provide adapter for the GridView, and the key to this example is to dynamically calculate the list of programs that the Baseadapter should display based on the user's single button. The activity code of the program is as follows

Program list:/viewswitcherdemo/src/com/example/viewswitcherdemo/mainactivity.java

/** * Does not accumulate Kuibu not even thousands of miles. * @author Peter. * */public class Mainactivity extends Activity {//define a constant to show the number of applications per scene private static final int number_per_screen = 12;/ /represents the application's internal class public static class DataItem {//Application name public String dataname;//application icon public drawable drawable;} Save the list collection for all applications of the system private arraylist<dataitem> items = new arraylist<mainactivity.dataitem> ();// Record the program that is currently showing the number of screens private int screenno = -1;//The total screen of the Save program private int screencount;    Viewswitcher switcher;//Create Layoutinflater object Layoutinflater inflater;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Inflater = Layoutinflater.from (mainactivity.this);        Creates a list collection of 40 elements that simulates a 40 application for (int i = 0; i < i++) {String label = "" + I;        drawable drawable = Getresources (). getdrawable (R.drawable.ic_launcher);        DataItem item = new DataItem (); Item.dataname = LabeL        item.drawable = drawable;        Items.Add (item);        }//Calculate the total number of screens that the application occupies//If the number of applications can be divisible by number_per_screen, the result of division is total number of screens//if not divisible, the total number of screens should be the result of Division plus 1.         Screencount = items.size ()% Number_per_screen = = 0?        Items.size ()/NUMBER_PER_SCREEN:items.size ()/Number_per_screen + 1;        Switcher = (viewswitcher) Findviewbyid (R.id.viewswitcher); Switcher.setfactory (New Viewfactory () {//actually returns a GridView component @overridepublic View Makeview () {//Load R.layout.slidel        Istview component, which is actually a GridView component return Inflater.inflate (R.layout.slidelistview, null);});    Display first screen next (null) when the page loads;    } public void Next (View v) {if (Screenno < screenCount-1) {Screenno + +;    Animate the Viewswitcher component display process switcher.setinanimation (this, r.anim.slide_in_right);    Sets the animation switcher.setoutanimation (this, r.anim.slide_out_left) for the Viewswitcher component hiding procedure;    (GridView) Switcher.getnextview ()). Setadapter (adapter); Switcher.shownext ();   }} public void Prev (View v) {if (Screenno > 0) {screenno--; Animate Switcher.setinanimation (this, Android) for the Viewswitcher component display process.    R.anim.slide_in_left); Set the animation switcher.setoutanimation (this, Android) for the Viewswitcher component hide process.    R.anim.slide_out_right);    Controls the adapter ((GridView) Switcher.getnextview ()) corresponding to the GridView that will be displayed on the next screen. Setadapter (adapter);    Switcher.showprevious (); }} private Baseadapter adapter = new Baseadapter () {@Override public int getcount () {//If it has reached the last screen, and    The number of applications cannot be divisible number_per_screen if (Screenno = = screenCount-1 && items.size ()% Number_per_screen! = 0) {    The last screen shows the number of programs for the Number_per_screen of the application to Items.size ()% Number_per_screen;    }//Otherwise the number of programs displayed per screen is Number_per_screen return number_per_screen; } @Override public DataItem getItem (int position) {//Based on Screenno calculates data for position list items return Items.get (scree    NNo * number_per_screen + position); } @Override Public LonG Getitemid (int position) {return position; } @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {View view = convertview;if (Convertview = = null) {//Load R.layout.labelicon layout File view = inflater.inflate (R.layout.labelicon, null);} Gets the ImageView component in the R.layout.labelicon layout file and sets the icon for it imageView ImageView = (imageView) View.findviewbyid (R.id.imageview) ; Imageview.setimagedrawable (GetItem (position) drawable);//Get TextView TextView = (TextView) View.findviewbyid ( R.id.textview) Textview.settext (GetItem (position); dataname); return view;}};}
GridView Layout File Code:

Program list:/viewswitcherdemo/res/layout/slidelistview.xml

<?xml version= "1.0" encoding= "Utf-8"? ><gridview  xmlns:android= "http://schemas.android.com/apk/res/ Android "  android:layout_width=" match_parent "  android:numcolumns=" 4 "  android:layout_height=" Match_ Parent "></GridView>
GridView layout code for each item:

Program list:/viewswitcherdemo/res/layout/labelicon.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:gravity= "Center"    android:orientation= "vertical" >    <imageview         android:id= "@+id/imageview"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        />    <textview         android: Id= "@+id/textview"        android:layout_height= "wrap_content"        android:layout_width= "Fill_parent        " android:gravity= "Center"        /></linearlayout>
When the user stand-alone ">" button, the program's event handler will control the Viewswitcher call Shownext () method to display the next screen of the program list-and resign Screenno is added 1, so adapter will dynamically calculate the next screen of the program list, The adapter is then passed to the GridView Viewswitcher to be displayed next.

In order to realize the animation effect of Viewswitcher switching view, the program event handling method calls the two animation resources provided by Android system, and also provides the animation resources by itself.

Where the R.anim.slide_in_right animation resource corresponds to the following code:

Program list:/viewswitcherdemo/res/anim/slide_in_right.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <!--settings Drag the animation from the right--    <translate         android:fromxdelta= "100%"        android:toxdelta= "0"        android:duration= "@android: Integer/config_mediumanimtime"        /></set>


Where the R.anim.slide_out_left animation resource corresponds to the following code:

List of programs:

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <!--settings Drag the animation from the left--    <translate         android:fromxdelta= "0"        android:toxdelta= " -100%p"        android:duration= "@android: Integer/config_mediumanimtime"        /></set>
After running the program, the following effects are shown:


You can see animation effects when switching

Do not understand the place can add I qq:798628358









function and usage of Viewswitcher view

Related Article

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.