Android source Deskclock (ii)

Source: Internet
Author: User
Tags switch case

I. Overview
in Deskclock (a) introduced the program source of the move out, and now began to analyze the application of the source code, Deskclock main has four functions, alarm clock, clock, timing, and stopwatch, in this blog main analysis of the Deskclock portal and the main UI logical structure, These four functions are strung together in subsequent series.
Two. Source Code Analysis
1.activity-alias Multi-entry configuration

When the app was installed, some apps generated two icons on the desktop, some of which were the same activity entry, and some of the other activity's portals, how did that happen? Look at the Android native Deskclock program and see the implementation of this feature. Using Activity-alias:

1). Syntax format

<activity-alias android:enabled=["true" | "false"]                android:exported=["true" | "false"]                android:icon= "drawable resource"                android:label= "string resource"                android:name= "string"                Android:permission= "string"                android:targetactivity= "string" >    : .</activity-alias>

2). Application in Deskclock

As you can see from the configuration below, this is the two entry for the same activity (Deskclock), and the names of both portals are the same, what is the point of doing this? You can see that a category named Android.intent.category.DESK_DOCK is marked in Activity-alias, which triggers the alias entry when the Android device is plugged into the desktop dock.

<activity android:name= "Deskclock" android:label= "@string/app_label" android:theme= "@style/deskclock" android:icon= "@mipmap/ic_launcher_alarmclock" android:launchmode= "Singletask" > <intent-f ilter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.in Tent.category.DEFAULT "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-fi Lter></activity><activity-alias android:name= "Dockclock" android:targetactivity= "DeskClock" an Droid:label= "@string/app_label" android:theme= "@style/deskclock" android:icon= "@mipmap/IC_LAUNCHER_ALARMCL Ock "android:launchmode=" Singletask "android:enabled=" @bool/config_dockappenabled "> <in tent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android Oid.intent.category.DEFAULT "/> <category android:name=" Android.intent.category.DESK_DOCK "/> </intent-filter></acti Vity-alias>

Activity-alias determines the activity of the portal connection by specifying the targetactivity, changes a different label (Clockalias) and icon (Chrysanthemum) to the program, and replaces the category of the dock base, As shown in the lower code configuration.

<activity-alias android:name= "Dockclock"         android:targetactivity= "Deskclock"         android:label= "@string/ App_second_label "         android:theme=" @style/deskclock "         android:icon=" @mipmap/entrance "         android: Launchmode= "Singletask"         >     <intent-filter>         <action android:name= " Android.intent.action.MAIN "/>         <category android:name=" Android.intent.category.DEFAULT "/>         < Category android:name= "Android.intent.category.LAUNCHER"/>     </intent-filter></activity-alias >

In this way, the configuration can be implemented in the Android device on the dual portal icon, click on the two icons can be entered into the Deskclock program, the specific effect as shown




2. Main structure of the homepageThe home page is primarily composed of action Bar, Viewpager, and Fragmentpageradapter, with four action bar tabs and abstract fragment defined by a custom Fragmentpagerada Pter adapter to complete the switchover of the four main functions in the Deskclock.
when Deskclock is created, a Actionbar is selected to display the identity location, And then to initialize the views. It is important to note that the Viewpager is set by the Setoffscreenpagelimit method to pre-load the fragment, here is the four main fragment,deskclock show a fragment, Preload another 3 fragment to improve the smoothness of the UI display.
        if (Mtabsadapter = = null) {            Mviewpager = new Viewpager (this);            Mviewpager.setid (R.id.desk_clock_pager);            Keep all four tabs to minimize Jank.            Mviewpager.setoffscreenpagelimit (3);            Mtabsadapter = new Tabsadapter (this, mviewpager);            Createtabs (Mselectedtab);        }        Setcontentview (Mviewpager);        Mactionbar.setselectednavigationitem (Mselectedtab);
Most of the logic, such as Main page transitions in the home page, is in the custom Fragmentpageradapter, where the main analysis is the adapter. Tabsadapter in the construction of the time can get to Deskclock Context,actionbar,viewpager and bind the adapter, binding page changes in the monitoring.
        Public Tabsadapter (activity activity, Viewpager Pager) {            super (Activity.getfragmentmanager ());            Mcontext = activity;            Mmainactionbar = Activity.getactionbar ();            Mpager = pager;            Mpager.setadapter (this);            Mpager.setonpagechangelistener (this);        }
it defines a tabinfo inner class that marks the properties and characteristics of each itemview, constructs the Tabinfo when the adapter item is populated, and binds the tabinfo to the corresponding Actionbar tab.
        Final class Tabinfo {            private final class<?> clss;            Private final Bundle args;            Tabinfo (class<?> _class, int position) {                clss = _class;                args = new Bundle ();                Args.putint (Key_tab_position, POSITION);            }            public int getPosition () {                return args.getint (key_tab_position, 0);            }        }
        public void AddTab (Actionbar.tab Tab, class<?> clss, int position) {            Tabinfo info = new Tabinfo (CLSS, position) ;            Tab.settag (info);            Tab.settablistener (this);            Mtabs.add (info);            Mmainactionbar.addtab (tab);            Notifydatasetchanged ();        }
when tab is selected, since the details of fragment have been bound to the tab, you can get all the information of tabinfo (mainly position) by tab. The fragment can then be converted based on location information. But what is this getrtlposion? RTL is right-to-left, most of the world's reading habits are ltr, but also a small number of areas have rtl,google of course also to be compatible with these users, provided RTL support. Getrtlposion method is when the local does not support RTL, first to determine whether there is no RTL set in the configuration file, if you set up a switch case to turn page item position way to provide RTL support, Although this technology is generally not used in the country, but can also be a simple look.when the page changes, the Actionbar can be changed directly via position notification.
        public void ontabselected (Tab tab, fragmenttransaction ft) {            Tabinfo info = (tabinfo) tab.gettag ();            int position = Info.getposition ();            Mpager.setcurrentitem (getrtlposition (position));        }
 public void onpageselected (int position) {//Set the page before doing T            He menu so, Oncreateoptionsmenu knows what page it is.            Mmainactionbar.setselectednavigationitem (getrtlposition (position));            Notifypagechanged (position);            Only show the overflow menu to alarm and world clock.                if (mmenu! = null) {//Make sure the menu ' s been initialized. if (position = = Alarm_tab_index | | position = = clock_tab_index) {mmenu.setgroupvisible (r.id.menu_items                    , true);                Oncreateoptionsmenu (Mmenu);                } else {mmenu.setgroupvisible (R.id.menu_items, false); }            }        }
 private Boolean Isrtl () {return Textutils.getlayoutdirectionfromlocale (L        Ocale.getdefault ()) = = View.layout_direction_rtl;                    } private int getrtlposition (int position) {if (Isrtl ()) {switch (position) {                    Case Timer_tab_index:return Rtl_timer_tab_index;                    Case Clock_tab_index:return Rtl_clock_tab_index;                    Case Stopwatch_tab_index:return Rtl_stopwatch_tab_index;                    Case Alarm_tab_index:return Rtl_alarm_tab_index;                Default:break;        }} return position; }
Tabsadapter in the same time loading different fragment, through Positon to obtain taginfo data, According to the instantiate method of fragment, the fragment of ClassLoader is compared with positon.
        Public Fragment getItem (int position) {            Tabinfo info = mtabs.get (getrtlposition (position));            Deskclockfragment f = (deskclockfragment) fragment.instantiate (                    Mcontext, Info.clss.getName (), Info.args);            return f;        }
Tabsadapter also has a page change monitoring registration and logoff, when there is a page change Tabsadapter will notify the change to register the listener fragment, in this program there are two functions fragment (timers and stopwatch) Different business logic needs to be handled according to the state of the page changes, which will be analyzed when the following two functions are described.
 public void Registerpagechangedlistener (Deskclockfragment frag) {String            Tag = Frag.gettag ();            if (mfragmenttags.contains (tag)) {LOG.WTF (Log_tag, "Trying to add an existing fragment" + tag);            } else {Mfragmenttags.add (Frag.gettag ()); }//Since registering a listener by the fragment are done sometimes after the page/were already CH Anged, make sure the fragment and the current page frag.onpagechanged (Mmainactionbar.getselectednavigationinde        X ()); } public void Unregisterpagechangedlistener (Deskclockfragment frag) {mfragmenttags.remove (Frag.gettag ()        ); }
        private void notifypagechanged (int newPage) {for            (String tag:mfragmenttags) {                final fragmentmanager fm = Getfra Gmentmanager ();                Deskclockfragment f = (deskclockfragment) fm.findfragmentbytag (tag);                if (f! = null) {                    f.onpagechanged (newPage);}}        }
Three. Summary

This blog mainly describes the main UI part of the Deskclock program logic, the main analysis of two points (multi-portal configuration and Viewpager,ActionBar, adapter switching and data binding) now the four functional parts are not covered, The following series will begin to introduce four functions.



Reprint Please specify source: http://blog.csdn.net/l2show/article/details/46722999

Android source Deskclock (ii)

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.