android-Improvement << Imitation qq>> frame source

Source: Internet
Author: User

The article is mainly modified in csdn a great god of an article, I think this article is very thorough object-oriented, below to share the following points to learn:

Android app one of the classic main interface framework: Imitation QQ (using fragment, with source code)

1. Calculate and optimize by & symbol: ( later by asking colleagues, said that the computer through the bit operation efficiency than usual switch efficiency, and explained the principle of the algorithm. )

public class Constant {public static final int sign_fragment_message=0x01 <<1;public static final int Sign_fragment_ contacts=0x01 <<2;public static final int sign_fragment_news=0x01 <<3;public static final int Sign_fragment_ SETTENGS=0X01 <<4;}

@Overridepublic void onclickcallback (int itemID) {String tag = ""; if (ItemID & constant.sign_fragment_message)! = 0) {tag = constant.str_fragment_message;} else if ((ItemID & constant.sign_fragment_contacts)! = 0) {tag = constant.str_fragment_contacts;} else if (ItemID &am P constant.sign_fragment_news)! = 0) {tag = constant.str_fragment_news;} else if (ItemID & Constant.sign_fragment_ settengs)! = 0) {tag = constant.str_fragment_settings;} Mheaderpanellayout.settext (tag); settabsection (tag);}

2. "Dynamic" adjustment of buttons in the middle of the bottom bar via onlayout

@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); Layouti TEM (L, T, R, b);} private void Layoutitem (int left, int top, int right, int bottom) {int allchildwidth=0;int num=getchildcount (); for (int i = 0; i < num; i++) {Allchildwidth+=getchildat (i). GetWidth ();} int Absolutewidth=right-left-getpaddingleft ()-getpaddingright (), int blankwidth= (absolutewidth-allchildwidth)/( NUM-1)///Set 2nd 3 button spacing Layoutparams params1= (layoutparams) mcontactsbtn.getlayoutparams ();p arams1.leftmargin= Blankwidth;mcontactsbtn.setlayoutparams (PARAMS1); Layoutparams params2= (Layoutparams) mnewsbtn.getlayoutparams ();p arams2.leftmargin=blankwidth; Mnewsbtn.setlayoutparams (PARAMS2);}

3. Two instances of the application of the layout:

1) through Layoutinflater.

Public Imagetext (context context, AttributeSet Attrs) {Super (context, attrs); Layoutinflater.from (context). Inflate (R.layout.image_text_layout, this,true); mimageview= (ImageView) Findviewbyid ( R.id.iv_imgae_text); mtextiew= (TextView) Findviewbyid (r.id.tv_imgae_text);}

2) through Onfinishinflater ()

<?xml version= "1.0" encoding= "Utf-8"? ><org.lean.ui.bottompanellayout xmlns:android= "/http Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Wrap_content "and Roid:background= "#FFF3F3F3" android:paddingleft= "20DP" android:paddingright= "20DP" Android:layout_alignparentbott Om= "true" > <org.lean.ui.imagetext android:id= "@+id/message_btn" android:layout_width= "wrap_content "Android:layout_height=" Wrap_content "android:layout_alignparentleft=" true "/> <org.lean.ui.imaget Ext android:id= "@+id/contacts_btn" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "android:layout_torightof=" @id/message_btn "/> <org.lean.ui.imagetext android:id=" @+id/news_b TN "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_torightof = "@id/contacts_btn"/> <org.lean.uI.imagetext android:id= "@+id/settings_btn" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true"/></org.lean.ui.bottompanellayout>


@Overrideprotected void Onfinishinflate () {super.onfinishinflate (); mmessagebtn= (Imagetext) Findviewbyid ( R.ID.MESSAGE_BTN); mcontactsbtn= (Imagetext) Findviewbyid (R.ID.CONTACTS_BTN); mnewsbtn= (ImageText) findViewById ( R.ID.NEWS_BTN); msettingsbtn= (Imagetext) Findviewbyid (R.ID.SETTINGS_BTN); Initclickevent ();}

4. Proxy for data transfer (one of the most common design patterns in iOS)

public class Bottompanellayout extends Relativelayout implements Onclicklistener{private bottompanelcallbackprotocal mcallbackprotocal;//Proxy Protocol public void setcallbackprotocal (Bottompanelcallbackprotocal callbackprotocal) { this.mcallbackprotocal = callbackprotocal;} public interface bottompanelcallbackprotocal{public void Onclickcallback (int itemID); /** * 1. Modify the style itself * 2. External Declaration event */@Overridepublic void OnClick (View v) {initbottompanel (); int Index=-1;switch (V.getid ()) {CA Se r.id.message_btn:index=constant.sign_fragment_message;mmessagebtn.setchecked (index); Break;case R.id.contacts_ btn:index=constant.sign_fragment_contacts;mcontactsbtn.setchecked (index); Break;case R.id.news_btn:index= constant.sign_fragment_news;mnewsbtn.setchecked (index); Break;case R.id.settings_btn:index=constant.sign_ fragment_settengs;msettingsbtn.setchecked (index); break;default:break;} if (mcallbackprotocal!=null) {mcallbackprotocal.onclickcallback (index);}}}


public class Mainactivity extends Activity implementsbottompanelcallbackprotocal {@Overridepublic void Onclickcallback (int ItemID) {String tag = ""; if ((ItemID & constant.sign_fragment_message)! = 0) {tag = constant.str_fragment_message;} else if ((ItemID & constant.sign_fragment_contacts)! = 0) {tag = constant.str_fragment_contacts;} else if (ItemID &am P constant.sign_fragment_news)! = 0) {tag = constant.str_fragment_news;} else if (ItemID & Constant.sign_fragment_ settengs)! = 0) {tag = constant.str_fragment_settings;} Mheaderpanellayout.settext (tag); settabsection (tag);}}

5. Modify the original fragment jump code(The previous method Ensuretransaction () is to be judged at the time of pasting or elimination, but as a transaction, it is only necessary to make sure that the thing is only one start, without having to call it every time)

private void Settabsection (String tag) {if (Textutils.equals (tag, currfagtag)) {return;} Ensuretransaction (); if (Currfagtag! = null &&!currfagtag.equals ("")) {detachfragment (Getfragment (Currfagtag ));} Attachfragment (R.id.fragment_panel, getfragment (tag), tag); CommitTransaction ();}

private void Ensuretransaction () {if (mfragmenttransaction = = null) {mfragmenttransaction = Mfragmentmanager.begintransaction (); Mfragmenttransaction.settransition (Fragmenttransaction.transit_fragment_ FADE);}}

6.Fragment fragment to jump and pass the value of the improvement. (here the experiment from Messagefragment click TextView Jump to contactfragment);

1> in the messagefragment.

@Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); Getactivity (). Findviewbyid (R.ID.MSG_TV). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick ( View v) {((mainactivity) getactivity ()). Settabsection (constant.str_fragment_contacts);}});

2> inin ContactfragmentDeclaring data proxies

Declares a variable that stores all the parameters required by the fragment to manually invoke its update data when the view is refreshed private contactfragmentcallback mcontactfragmentcallback;// Declares that the interface public interface contactfragmentcallback{//describes the fragment update requires a String object public string Getcontentstr ();}

3>messagefragment Implement the agent

public class Messagefragment extends Basefragment implements contactfragmentcallback{@Overridepublic String Getcontentstr () {return "ABC";}}

4> inin ContactfragmentCallback

@Overridepublic void Onresume () {super.onresume (); mainactivity.currfagtag=constant.str_fragment_contacts;//by removing the data stored in the last FRAGMENT FRAGMENT f= ((mainactivity) Getactivity ()). Getfragment (Constant.str_fragment_message); if (F!=null&&f instanceof Contactfragmentcallback) {mcontactfragmentcallback= (contactfragmentcallback) F; TextView textview= (TextView) ((mainactivity) getactivity ()). Findviewbyid (R.ID.CONTACT_TV); Textview.settext ( Mcontactfragmentcallback.getcontentstr ());}}



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.