Android QQ空間(Apad)項目總結(三)---應用UI框架的搭建!!!

  

大家好,今天是元旦節了,祝大家節日快樂!今天給大家分享的是Apad Qzone的UI框架,我們首先看下交互圖如下:

圖1:交互效果圖.

從上圖可以看出,整個應用其實UI框架相對比較簡單,可以分爲倆部分,左側導航欄區域,右側顯示內容區域。當我們點擊左側導航欄時,右側顯示相對應內容。

應用的主要內容分爲四個模塊:好友動態;個人主頁;好友列表;應用中心。右側顯示內容則統一由一個管理器管理,管理器管理了右側的容器以及顯示內容面板。

也許用文字不太好說清楚,所以我寫了一個簡單的Demo以及畫了一個UI結構圖方便大家理解:

首先是新建一個Android工程,命名爲QzoneFrameDemo,結構如下:

圖2:程序代碼結構圖:

爲了更容易理解代碼,我畫了一個各個類的關係圖如下:

上圖可以清晰的看清各個類之間的關係,其中QZRightWindowManger管理了QZRightWindowContainer(剪頭忘記加了)和右側的四個Window,QZRightWindowContainer繼承了FrameLayout,四個Window繼承了QZRightWindowBase。

其中QZRightWindowContainer代碼如下(繼承了FrameLayout):

  1. package com.tutor.frame;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.FrameLayout;  
  6.   
  7. public class QZRightWindowContainer extends FrameLayout {  
  8.   
  9.     public QZRightWindowContainer(Context context){  
  10.         super(context);  
  11.     }  
  12.     public QZRightWindowContainer(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.   
  16. }  


而右側四個Window的基類QZRightWindowBase的代碼如下:

  1. package com.tutor.frame;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.FrameLayout;  
  6. import android.widget.TextView;  
  7.   
  8. public abstract class QZRightWindowBase extends FrameLayout {  
  9.   
  10.     public TextView mContentTextView;  
  11.       
  12.     private LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,  
  13.             LayoutParams.FILL_PARENT);  
  14.           
  15.     public QZRightWindowBase(Context context){  
  16.         super(context);  
  17.         setupViews();  
  18.     }  
  19.       
  20.     public QZRightWindowBase(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.         setupViews();  
  23.     }  
  24.       
  25.     private void setupViews(){  
  26.         mContentTextView = new TextView(getContext());  
  27.         mContentTextView.setLayoutParams(params);  
  28.     }  
  29.       
  30.     //做些事爲了擴展舉例而已   
  31.     public abstract void dosomething();  
  32.     //做些事2   
  33.     public abstract void dosomething2();  
  34.   
  35. }  
右側窗口Window1即QZRightWindow1代碼(其他的都一樣不貼代碼了)如下:

  1. package com.tutor.frame;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Color;  
  5. import android.util.AttributeSet;  
  6.   
  7. public class QZRightWindow1 extends QZRightWindowBase{  
  8.   
  9.     public QZRightWindow1(Context context){  
  10.         super(context);  
  11.         setupViews();  
  12.     }  
  13.     public QZRightWindow1(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         setupViews();  
  16.     }  
  17.       
  18.     private void setupViews(){  
  19.         mContentTextView.setText("好友動態");  
  20.         mContentTextView.setBackgroundColor(Color.RED);  
  21.         addView(mContentTextView);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void dosomething() {  
  26.         // TODO Auto-generated method stub   
  27.           
  28.     }  
  29.   
  30.     @Override  
  31.     public void dosomething2() {  
  32.         // TODO Auto-generated method stub   
  33.           
  34.     }  
  35.   
  36. }  

管理QZRightWindowContainer和右側四個Window的管理類QZRightWindowManager代碼如下:

  1. package com.tutor.frame;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Iterator;  
  5.   
  6. import android.view.View;  
  7.   
  8.   
  9.   
  10.   
  11. public class QZRightWindowManager {  
  12.   
  13.      /** 
  14.      * 好友動態面板的KEY 
  15.      */  
  16.     public static final int FRIEND_TRENDS_WINDOW = 0;  
  17.       
  18.      /** 
  19.      * 個人中心面板的KEY 
  20.      */  
  21.     public static final int HOME_PAGE_WINDOW = 1;  
  22.       
  23.      /** 
  24.      * 好友關係鏈面板的KEY 
  25.      */  
  26.     public static final int FRIEND_LIST_WINDOW = 2;  
  27.       
  28.      /** 
  29.      * 應用中心面板的KEY 
  30.      */  
  31.     public static final int APP_CENTER_WINDOW = 3;  
  32.       
  33.       
  34.     private HashMap<Integer, QZRightWindowBase> mHashMap;  
  35.       
  36.     private QZRightWindowContainer mContainer;  
  37.       
  38.       
  39.     public QZRightWindowManager(){  
  40.         mHashMap = new HashMap<Integer, QZRightWindowBase>();   
  41.     }  
  42.       
  43.     public void setmContainer(QZRightWindowContainer container) {  
  44.         this.mContainer = container;  
  45.     }  
  46.       
  47.     public void showRightWindow(int num,QZRightWindowBase mQzRightWindowBase){  
  48.         if(!mHashMap.containsKey(num)){  
  49.             mHashMap.put(num, mQzRightWindowBase);  
  50.             if(!(mQzRightWindowBase instanceof QZRightWindow1)){  
  51.                 mContainer.addView(mQzRightWindowBase);  
  52.             }  
  53.         }  
  54.           
  55.         for (Iterator iter = mHashMap.keySet().iterator(); iter.hasNext();) {  
  56.             Object key = iter.next();  
  57.             QZRightWindowBase qzb = mHashMap.get(key);  
  58.             qzb.setVisibility(View.INVISIBLE);  
  59.         }  
  60.           
  61.         mQzRightWindowBase.setVisibility(View.VISIBLE);  
  62.     }  
  63.       
  64. }  

主程序QzoneFrameDemoActivity代碼如下:

  1. package com.tutor.framedemo;  
  2.   
  3. import com.tutor.frame.QZLeftNavBar;  
  4. import com.tutor.frame.QZRightWindow1;  
  5. import com.tutor.frame.QZRightWindow2;  
  6. import com.tutor.frame.QZRightWindow3;  
  7. import com.tutor.frame.QZRightWindow4;  
  8. import com.tutor.frame.QZRightWindowBase;  
  9. import com.tutor.frame.QZRightWindowContainer;  
  10. import com.tutor.frame.QZRightWindowManager;  
  11.   
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16.   
  17. public class QzoneFrameDemoActivity extends Activity implements OnClickListener{  
  18.       
  19.     private QZRightWindow1 mQzRightWindow1;  
  20.       
  21.     private QZRightWindow2 mQzRightWindow2;  
  22.       
  23.     private QZRightWindow3 mQzRightWindow3;  
  24.       
  25.     private QZRightWindow4 mQzRightWindow4;  
  26.       
  27.     private QZLeftNavBar mQzLeftNavBar;  
  28.       
  29.     private QZRightWindowContainer mQzRightWindowContainer;  
  30.       
  31.     private QZRightWindowManager mQzRightWindowManager;  
  32.       
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.           
  38.         setupViews();  
  39.     }  
  40.       
  41.     private void setupViews(){  
  42.         mQzRightWindowManager = new QZRightWindowManager();  
  43.           
  44.         mQzLeftNavBar = (QZLeftNavBar)findViewById(R.id.navbar);  
  45.           
  46.         mQzLeftNavBar.findViewById(R.id.rw1).setOnClickListener(this);  
  47.         mQzLeftNavBar.findViewById(R.id.rw2).setOnClickListener(this);  
  48.         mQzLeftNavBar.findViewById(R.id.rw3).setOnClickListener(this);  
  49.         mQzLeftNavBar.findViewById(R.id.rw4).setOnClickListener(this);  
  50.           
  51.         mQzRightWindow1 = (QZRightWindow1)findViewById(R.id.qzrw1);  
  52.           
  53.         mQzRightWindowContainer = (QZRightWindowContainer)findViewById(R.id.container);  
  54.         mQzRightWindowManager.setmContainer(mQzRightWindowContainer);  
  55.     }  
  56.   
  57.     private void showRightWindow(int num,QZRightWindowBase mQzRightWindowBase){  
  58.         mQzRightWindowManager.showRightWindow(num, mQzRightWindowBase);  
  59.     }  
  60.       
  61.     @Override  
  62.     public void onClick(View v) {         
  63.         int id = v.getId();  
  64.         switch (id) {  
  65.         case R.id.rw1:  
  66.             showRightWindow(QZRightWindowManager.FRIEND_TRENDS_WINDOW, mQzRightWindow1);  
  67.             break;  
  68.         case R.id.rw2:  
  69.             if(mQzRightWindow2 == null){  
  70.                 mQzRightWindow2 = new QZRightWindow2(this);  
  71.             }  
  72.             showRightWindow(QZRightWindowManager.HOME_PAGE_WINDOW, mQzRightWindow2);  
  73.             break;  
  74.         case R.id.rw3:  
  75.             if(mQzRightWindow3 == null){  
  76.                 mQzRightWindow3 = new QZRightWindow3(this);  
  77.             }  
  78.             showRightWindow(QZRightWindowManager.FRIEND_LIST_WINDOW, mQzRightWindow3);  
  79.             break;  
  80.         case R.id.rw4:  
  81.             if(mQzRightWindow4 == null){  
  82.                 mQzRightWindow4 = new QZRightWindow4(this);  
  83.             }  
  84.             showRightWindow(QZRightWindowManager.APP_CENTER_WINDOW, mQzRightWindow4);  
  85.             break;  
  86.         default:  
  87.             break;  
  88.         }  
  89.     }  
  90. }  

主程序所用到的佈局文件main.xml代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <com.tutor.frame.QZLeftNavBar  
  8.         android:id="@+id/navbar"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="fill_parent"/>  
  11.   
  12.     <com.tutor.frame.QZRightWindowContainer  
  13.         android:id="@+id/container"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="fill_parent"  
  16.      >  
  17.         <com.tutor.frame.QZRightWindow1  
  18.              android:id="@+id/qzrw1"  
  19.              android:layout_width="fill_parent"  
  20.              android:layout_height="fill_parent"  
  21.           />  
  22.      </com.tutor.frame.QZRightWindowContainer>  
  23. </LinearLayout>  

運行效果如下:

效果1

效果2.

OK,這樣就大功告成了!對於pad上面的應用,單Activity化,各個功能模塊化,UI控件化,是比較好的選擇,這樣可以加大開發效率,減少和其他同學的耦合性。

下面的鏈接是源代碼,供新手們學習用,今天就講到這裏,謝謝大家!!!

源代碼點擊進入==>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章