Android-improved & lt; imitation QQ & gt; Framework source code, android-source code

Source: Internet
Author: User

Android-improved <imitation QQ> framework source code, android-source code

This article is mainly modified in an article by a major CSDN expert. I think this article is very object-oriented. The following are some of the points to learn:

One of the classic main interface frameworks of Android applications: Imitation QQ (use Fragment, with source code)

1. Computing optimization through & Symbol:(Later, I asked my colleagues to say that the bit operation efficiency of the computer is higher than the normal 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 & 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. Use onLayout to adjust the buttons in the middle of the Bottom Bar dynamically.

@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B); layoutItem (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 the spacing of the 2nd three buttons to LayoutParams params1 = (LayoutParams) mContactsBtn. getLayoutParams (); params1.leftMargin = blankWidth; mContactsBtn. setLayoutParams (params1); LayoutParams params2 = (LayoutParams) mNewsBtn. getLayoutParams (); params2.leftMargin = blankWidth; mNewsBtn. setLayoutParams (params2 );}

3. Applications with two instantiation la s:

1) Use 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) use 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"    android:background="#FFF3F3F3"    android:paddingLeft="20dp"    android:paddingRight="20dp"    android:layout_alignParentBottom="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.ImageText        android:id="@+id/contacts_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/message_btn" />    <org.lean.ui.ImageText        android:id="@+id/news_btn"        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. data transmission by proxy(The most common design mode 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 * 2. declare events externally */@ Overridepublic void onC Lick (View v) {initBottomPanel (); int index =-1; switch (v. getId () {case 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_SETT ENGS; 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 & 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 code of the original Fragment jump.(The previous method ensureTransaction () is to be judged when it is pasted or eliminated, but as a transaction, you only need to ensure that the transaction has only one start, instead of calling 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: improved the method of redirecting to Fragment and passing values.(Here we will test the jump from MessageFragment to textview to ContactFragment );

1> In 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> declare data proxy in ContactFragment

// Declare a variable that stores all the parameters required by the Fragment. When the View is refreshed, it is manually called to update the data private ContactFragmentCallBack mContactFragmentCallBack; // declare the public interface ContactFragmentCallBack {// indicates that a String object public String getContentStr () is required for the Fragment update ();}

3> MessageFragment implements this proxy

public class MessageFragment extends BaseFragment implements ContactFragmentCallBack{@Overridepublic String getContentStr() {return "abc";}}

4> callback in ContactFragment

@ Overridepublic void onResume () {super. onResume (); MainActivity. currFagTag = Constant. STR_FRAGMENT_CONTACTS; // retrieve the Data Fragment f = (MainActivity) getActivity () stored in the previous Fragment ()). 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 ());}}

Source code of the modified project



This is the android-like QQ chat source code on the Internet, which is 388M, but the client is wrong in my environment. Why? The SDK is 42

1. Create a project,
2 ctrl + e open to this project
3. Delete assets, src, res
4. Copy the chat project assets, src, res, libs, and AndroidManifest. xml to this project.

In 5eclipse, right-click the new project refresh and check the error message.

Android imitation QQ code running problems

When you import the file, you just need to import the java document. Now the android version is basically above 4.0, and your client program cannot run on the simulator. You need to add the following code to loginactivity, when the code arrives, you will contact me and I will send it to you. My qq0000190963 and I will change the version to any number higher than 8 in the configuration file. Then start the server program and the client can download it to your mobile phone.

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.