Xsupermes Mobile Use fragmentactivity fit large screen (i)

Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left">Xsupermes Mobile uses fragmentactivity to fit Large-screen</p></p><p align="left"><p align="left">Xsupermes Mobile runs on a wide variety of devices, with small screen phones, oversized flat panels and even tvs. For the screen size gap, in many cases, the first to develop a set of apps for the mobile phone, and then copy one, modify the layout to adapt to what super large Screen. Can't you do an app that adapts to both mobile and tablet? The answer is, of course, that Fragment.fragment's original intention was to solve the Problem.</p></p><p align="left"><p align="left">You can think of fragment as part of the activity interface, even the activity interface is composed of completely different fragment, and the more handsome is fragment has its own declaration cycle and receive, handle the User's events, This eliminates the need to write a bunch of event, control code in an Activity. More importantly, you can dynamically add, replace, remove a Fragment.</p></p><p><p>1. How to use fragment activity</p></p><p><p>To use the Fragment function must first let the activity inherit fragmentactivity, the reason is that it contains fragment Operation Fragmentmanager interface implementation class fragmentmanagerimpl, This class manages the display and hiding of all fragment</p></p><p><p>1. Using the simplest fragment, we just have to inherit fragment to</p></p><p><p>public class Textfragment extends fragment{</p></p><p><p></p></p><p><p>Private String mmsg;</p></p><p><p></p></p><p><p>public void Setmessage (String Message) {</p></p><p><p>This.mmsg = message;</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>Public View oncreateview (layoutinflater inflater, viewgroup container,</p></p><p><p>Bundle Savedinstancestate) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Final context context = Getactivity ();</p></p><p><p>Framelayout root = new Framelayout (context);</p></p><p><p>Root.setbackgroundcolor (color.yellow);</p></p><p><p>TextView TV = new TextView (context);</p></p><p><p>Tv.settext (mmsg);</p></p><p><p>Tv.setgravity (gravity.center);</p></p><p><p>Root.addview (tv, New Framelayout.layoutparams (</p></p><p><p>ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));</p></p><p><p>Return root;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>first, fragment can treat it as a view, except that the view has a life cycle function like Activity.</p></p><p><p></p></p><p><p></p></p><p><p>The Fragment.oncreateview () function is the view used to generate the Fragment layout, similar to the Baseadapter.getview ()</p></p><p><p>Such a simple layout with a TextView is Done.</p></p><p><p>2. Rewrite our own Fragmentactivity.</p></p><p><p>This is mainly through the Fragmentmanager to fragment to add and delete:</p></p><p><p>public class Textfragment extends fragment{</p></p><p><p></p></p><p><p>Private String mmsg;</p></p><p><p></p></p><p><p>public void Setmessage (String Message) {</p></p><p><p>This.mmsg = message;</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>Public View oncreateview (layoutinflater inflater, viewgroup container,</p></p><p><p>Bundle Savedinstancestate) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Final context context = Getactivity ();</p></p><p><p>Framelayout root = new Framelayout (context);</p></p><p><p>Root.setbackgroundcolor (color.yellow);</p></p><p><p>TextView TV = new TextView (context);</p></p><p><p>Tv.settext (mmsg);</p></p><p><p>Tv.setgravity (gravity.center);</p></p><p><p>Root.addview (tv, New Framelayout.layoutparams (</p></p><p><p>ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));</p></p><p><p>Return root;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>first, fragment can treat it as a view, except that the view has a life cycle function like Activity.</p></p><p><p>public class Doorfragmentactivity extends fragmentactivity{</p></p><p><p></p></p><p><p>public static final String frag_sms = "sms_list_frag";</p></p><p><p>public static final String Frag_text = "text_frag";</p></p><p><p></p></p><p><p>Private Fragment msmsfragment;</p></p><p><p>Private Fragment mtextfragment;</p></p><p><p>Private Fragmentmanager mfragmgr;</p></p><p><p></p></p><p><p>Private Button mmenubtn;</p></p><p><p></p></p><p><p>@Override</p></p><p><p>protected void OnCreate (Bundle Savedinstancestate) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Super.oncreate (savedinstancestate);</p></p><p><p>Setcontentview (r.layout.door_fragment_activity_layout);</p></p><p><p>Mfragmgr = Getsupportfragmentmanager ();</p></p><p><p>MMENUBTN = (Button) Findviewbyid (r.id.door_menu_btn);</p></p><p><p>Mmenubtn.setonclicklistener (new Onclicklistener () {</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public void OnClick (View V) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Showfragments (frag_text, true);</p></p><p><p>}</p></p><p><p>});</p></p><p><p>Mmenubtn.setonlongclicklistener (new Onlongclicklistener () {</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public boolean Onlongclick (View v) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p></p></p><p><p>Return false;</p></p><p><p>}</p></p><p><p>});</p></p><p><p>Initfragments ();</p></p><p><p>Showfragments (frag_sms, false);</p></p><p><p>}</p></p><p><p></p></p><p><p>private void Initfragments () {</p></p><p><p>msmsfragment = new Smslistfragment ();</p></p><p><p>Textfragment Textfrag = new Textfragment ();</p></p><p><p>Textfrag.setmessage ("this is the menu interface");</p></p><p><p>Mtextfragment = textfrag;</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>private void Showfragments (String tag, boolean Needback) {</p></p><p><p>Fragmenttransaction trans = Mfragmgr.begintransaction ();</p></p><p><p>If (needback) {</p></p><p><p>Trans.setcustomanimations (r.anim.frag_enter,</p></p><p><p>r.anim.frag_exit);</p></p><p><p>Trans.add (r.id.door_root_content_fl, Getfragmentbytag (tag), tag);</p></p><p><p>Trans.addtobackstack (tag);</p></p><p><p>}else{</p></p><p><p>Trans.replace (r.id.door_contents_fl, Getfragmentbytag (tag), tag);</p></p><p><p>}</p></p><p><p>Trans.commit ();</p></p><p><p>}</p></p><p><p></p></p><p><p>Private Fragment Getfragmentbytag (String Tag) {</p></p><p><p>If (frag_sms.equals (tag)) {</p></p><p><p>Return msmsfragment;</p></p><p><p>}</p></p><p><p>If (frag_text.equals (tag)) {</p></p><p><p>Return mtextfragment;</p></p><p><p>}</p></p><p><p>Return null;</p></p><p><p>}</p></p><p><p>}</p></p> <ul> <ul> <li>First we get the Fragmentmanager implementation: call Fragmentactivity.getsupportfragmentmanager () directly, Look at the source code can know that this return is Fragmentmanager internally defined implementation class Fragmentmanagerimpl.</li> <li>We didn't actually manipulate this class after we got fragmentmanagerimpl, just call Fragmentmanager.begintransation (), The implementation class that gets the Fragmenttransation interface (which is specifically an instance of the Backstackrecord class), and all of our operations on fragment are done through it, because without careful study, I only know that I am directly in the code to define fragment and not write in the XML (xml writing feels a bit awkward)</li> </ul> </ul><p align="left"><p align="left">We mainly deal with the fragment by some methods of fragmenttransation:</p></p><p><p>1) Trans.add (fragment, tag); This is actually CONTAINERVIEWID = 0 Call of 3)<br>2) Trans.add (containerviewid, fragment); This is actually the tag = null Call of 3)<br>3) trans.add (containerviewid, fragment, tag); If containerviewid! = 0 is actually called to get to the</p></p><p><p>The fragment Oncreateview method returns the view and joins the Containerviewid in this viewgroup to Viewgroup.addview (fragment.oncreateview ());</p></p><p><p>Unresolved issue: what does Containerviewid = 0 mean??</p></p><p><p>4) trans.replace (containerviewid, Fragment) is the same as null tag call 5)<br>5) trans.replace (containerviewid, fragment, Tag) This is the same as adding a fragment to the corresponding container, Just a step more than add to the same containerviewid in the existing fragment retrieval, removefragment operation, and then add the new fragment</p></p><p><p>6) Trans.addtobackstack (tag); If your fragment has an activity-like fallback response for the back key, remember to add it to the inside, and trans simulates the stack, but my fallback doesn't respond to the exit Anim I Set.</p></p><p><p>3. Again using the next listfragment, I write here is Smslistfragment inherited listfragment:</p></p><p><p>public class Smslistfragment extends listfragment{</p></p><p><p></p></p><p><p>Private Conversationlistadapter madapter;</p></p><p><p>Private Conversationquery mquery;</p></p><p><p>Private Long startTime;</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public void OnCreate (Bundle Savedinstancestate) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Super.oncreate (savedinstancestate);</p></p><p><p>Madapter = new Conversationlistadapter (getactivity ());</p></p><p><p>Mquery = new Conversationquery (getactivity (). getcontentresolver ());</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public void onactivitycreated (Bundle Savedinstancestate) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Super.onactivitycreated (savedinstancestate);</p></p><p><p>Setlistadapter (madapter);</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public void OnStart () {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Super.onstart ();</p></p><p><p>Startasyncquery ();</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>public void OnStop () {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>Super.onstop ();</p></p><p><p>Madapter.getcursor (). Close ();</p></p><p><p>Madapter.changecursor (null);</p></p><p><p>}</p></p><p><p></p></p><p><p>public void Startasyncquery () {</p></p><p><p>StartTime = System.currenttimemillis ();</p></p><p><p>Mquery.startquery (1, null, conversation.sallthreadsuri,</p></p><p><p>conversation.all_threads_projection, null, null,</p></p><p><p>conversation.conversation_order);</p></p><p><p>}</p></p><p><p></p></p><p><p>Private Final class Conversationquery extends Asyncqueryhandler {</p></p><p><p></p></p><p><p>Public Conversationquery (contentresolver Cr) {</p></p><p><p>Super (cr);</p></p><p><p>TODO auto-generated Constructor stub</p></p><p><p>}</p></p><p><p></p></p><p><p>@Override</p></p><p><p>protected void Onquerycomplete (int token, Object cookie, cursor Cursor) {</p></p><p><p>TODO auto-generated Method Stub</p></p><p><p>SYSTEM.OUT.PRINTLN ("conversation Cursor size:"</p></p><p><p>+ Cursor.getcount ());</p></p><p><p>Madapter.changecursor (cursor);</p></p><p><p>Toast.maketext (getactivity (),</p></p><p><p>"query the number of SMS sessions:" + cursor.getcount () + ", cost"</p></p><p><p>+ (system.currenttimemillis ()-startTime) + "ms",</p></p><p><p>Toast.length_long). Show ();</p></p><p><p>}</p></p><p><p>}</p></p><p><p>}</p></p><p align="left"><p align="left">There is no difference between the code and the use of ordinary listactivity,</p></p><p><p>OnCreate () to finish the thing that I want to start at once, I'm in There. basically initializing a adapter and a query to the SMS database</p></p><p><p>Set the adapter to the ListView in onactivitycreated (), and this uncertainty has no better position,</p></p><p><p>Then go to our familiar life cycle approach:</p></p><p><p>OnStart (), Open Query</p></p><p><p>OnStop (), Our interface is no longer displayed, so we don't care about database changes, close cursor</p></p><p><p>4. Layout files on the main page:</p></p><p><p><?xml version= "1.0" encoding= "utf-8"?></p></p><p><p><framelayout xmlns:android= "http://schemas.android.com/apk/res/android"</p></p><p><p>Android:id= "@+id/door_root_content_fl"</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "match_parent" ></p></p><p><p><linearlayout</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "match_parent"</p></p><p><p>android:orientation= "vertical" ></p></p><p><p><framelayout</p></p><p><p>Android:id= "@+id/door_contents_fl"</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "0dp"</p></p><p><p>android:layout_weight= "1"/></p></p><p><p><linearlayout</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "55dp"</p></p><p><p>android:orientation= "horizontal" ></p></p><p><p></p></p><p><p><button</p></p><p><p>Android:id= "@+id/door_menu_btn"</p></p><p><p>Android:layout_width= "match_parent"</p></p><p><p>android:layout_height= "match_parent"</p></p><p><p>android:gravity= "center"</p></p><p><p>android:text= "menu"/></p></p><p><p></LinearLayout></p></p><p><p></LinearLayout></p></p><p><p></FrameLayout></p></p><p><p></p></p><p><p>Xsupermes Mobile Use fragmentactivity fit large screen (i)</p></p></span>

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.