【0118】【項目實戰】-【組件化封裝思想實戰Android App】-【1】首頁框架搭建

1.項目簡單介紹

2.首頁框架搭建

2.1 功能及思路

2.2 框架搭建實戰

 

【命名規則】java

【包的命名】須要根據不一樣的功能命名;android

【類的命名】後綴以功能類型爲後綴;app

【佈局的xml的命名】所在包的功能-業務-控件的類型框架

【程序的入口】ide

 

【fragment】業務的fragment沒有直接繼承與fragment,而是在上面的一層進行了封裝BaseFragment;全部的項目都是這樣須要封裝的;佈局

【封裝的好處】在修改相關的fragment的業務的時候,只須要修改封轉好的類便可,沒有必要找到各個fragment都進行修改;性能

【Activity的封裝】ui

【HomeActivity】此處不使用註解框架:由於在大型app的開發的過程當中,註解框架對於性能是有損耗的,當出現了莫名其妙的問題的時候,排查問題很差排查;this

【HomeActivity的佈局】spa

  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2                 android:layout_width="match_parent"
  3                 android:layout_height="match_parent"
  4 >
  5 
  6     <RelativeLayout
  7         android:id="@+id/content_layout"
  8         android:layout_width="match_parent"
  9         android:layout_height="match_parent"
 10         android:layout_above="@+id/linearLayout"/>
 11 
 12     <LinearLayout
 13         android:id="@+id/linearLayout"
 14         android:layout_width="match_parent"
 15         android:layout_height="wrap_content"
 16         android:layout_alignParentBottom="true"
 17         android:background="@android:color/white"
 18         android:orientation="horizontal"
 19         android:paddingBottom="4dp"
 20         android:paddingTop="4dp">
 21 
 22         <RelativeLayout
 23             android:id="@+id/home_layout_view"
 24             android:layout_width="wrap_content"
 25             android:layout_height="wrap_content"
 26             android:layout_weight="1"
 27         >
 28 
 29             <TextView
 30                 android:id="@+id/home_image_view"
 31                 android:layout_width="wrap_content"
 32                 android:layout_height="wrap_content"
 33                 android:layout_centerHorizontal="true"
 34                 android:background="@drawable/comui_tab_home"
 35             />
 36 
 37             <TextView
 38                 android:layout_width="wrap_content"
 39                 android:layout_height="wrap_content"
 40                 android:layout_below="@+id/home_image_view"
 41                 android:layout_centerHorizontal="true"
 42                 android:layout_marginTop="4dp"
 43                 android:text="@string/home_image_view_text"
 44                 android:textColor="@color/color_333333"
 45                 android:textSize="16sp"
 46             />
 47         </RelativeLayout>
 48 
 49         <RelativeLayout
 50             android:id="@+id/pond_layout_view"
 51             android:layout_width="wrap_content"
 52             android:layout_height="wrap_content"
 53             android:layout_weight="1"
 54             android:visibility="gone"
 55         >
 56 
 57             <TextView
 58                 android:id="@+id/fish_image_view"
 59                 android:layout_width="wrap_content"
 60                 android:layout_height="wrap_content"
 61                 android:layout_centerHorizontal="true"
 62                 android:background="@drawable/comui_tab_pond"
 63             />
 64 
 65             <TextView
 66                 android:layout_width="wrap_content"
 67                 android:layout_height="wrap_content"
 68                 android:layout_below="@+id/fish_image_view"
 69                 android:layout_centerHorizontal="true"
 70                 android:layout_marginTop="4dp"
 71                 android:text="@string/pond_image_view_text"
 72                 android:textColor="@color/color_333333"
 73                 android:textSize="16sp"
 74             />
 75         </RelativeLayout>
 76 
 77         <RelativeLayout
 78             android:id="@+id/message_layout_view"
 79             android:layout_width="wrap_content"
 80             android:layout_height="wrap_content"
 81             android:layout_weight="1"
 82         >
 83 
 84             <TextView
 85                 android:id="@+id/message_image_view"
 86                 android:layout_width="wrap_content"
 87                 android:layout_height="wrap_content"
 88                 android:layout_centerHorizontal="true"
 89                 android:background="@drawable/comui_tab_message"
 90             />
 91 
 92             <TextView
 93                 android:layout_width="wrap_content"
 94                 android:layout_height="wrap_content"
 95                 android:layout_below="@+id/message_image_view"
 96                 android:layout_centerHorizontal="true"
 97                 android:layout_marginTop="4dp"
 98                 android:text="@string/message_image_view_text"
 99                 android:textColor="@color/color_333333"
100                 android:textSize="16sp"
101             />
102         </RelativeLayout>
103 
104         <RelativeLayout
105             android:id="@+id/mine_layout_view"
106             android:layout_width="wrap_content"
107             android:layout_height="wrap_content"
108             android:layout_weight="1"
109         >
110 
111             <TextView
112                 android:id="@+id/mine_image_view"
113                 android:layout_width="wrap_content"
114                 android:layout_height="wrap_content"
115                 android:layout_centerHorizontal="true"
116                 android:background="@drawable/comui_tab_home"
117             />
118 
119             <TextView
120                 android:layout_width="wrap_content"
121                 android:layout_height="wrap_content"
122                 android:layout_below="@+id/mine_image_view"
123                 android:layout_centerHorizontal="true"
124                 android:layout_marginTop="4dp"
125                 android:text="@string/mine_image_view_text"
126                 android:textColor="@color/color_333333"
127                 android:textSize="16sp"
128             />
129         </RelativeLayout>
130     </LinearLayout>
131 
132     <View
133         android:layout_width="match_parent"
134         android:layout_height="0.5dp"
135         android:layout_above="@+id/linearLayout"
136         android:background="@color/color_333333"
137     />
138 </RelativeLayout>

 

【源碼】Client_Code\app\src\main\java\com\youdu\activity\HomeActivity.java

  1 package com.youdu.activity;
  2 
  3 import android.app.Fragment;
  4 import android.app.FragmentManager;
  5 import android.app.FragmentTransaction;
  6 import android.content.Intent;
  7 import android.os.Bundle;
  8 import android.view.View;
  9 import android.view.View.OnClickListener;
 10 import android.widget.RelativeLayout;
 11 import android.widget.TextView;
 12 
 13 import com.youdu.R;
 14 import com.youdu.activity.base.BaseActivity;
 15 import com.youdu.service.UpdateProductService;
 16 import com.youdu.view.fragment.home.HomeFragment;
 17 import com.youdu.view.fragment.home.MessageFragment;
 18 import com.youdu.view.fragment.home.MineFragment;
 19 
 20 
 21 public class HomeActivity extends BaseActivity implements OnClickListener {
 22 
 23     private FragmentManager fm;
 24     private HomeFragment mHomeFragment;
 25     private Fragment mCommonFragmentOne;
 26     private MessageFragment mMessageFragment;
 27     private MineFragment mMineFragment;
 28     private Fragment mCurrent;
 29 
 30     private RelativeLayout mHomeLayout;
 31     private RelativeLayout mPondLayout;
 32     private RelativeLayout mMessageLayout;
 33     private RelativeLayout mMineLayout;
 34     private TextView mHomeView;
 35     private TextView mPondView;
 36     private TextView mMessageView;
 37     private TextView mMineView;
 38 
 39     @Override
 40     protected void onCreate(Bundle savedInstanceState) {
 41         super.onCreate(savedInstanceState);
 42         changeStatusBarColor(R.color.color_fed952);
 43         setContentView(R.layout.activity_home_layout);
 44         //啓動後臺產品服務更新
 45         startAllService();
 46         initView();
 47 
 48         mHomeFragment = new HomeFragment();   //新建home的activity;
 49         fm = getFragmentManager();  //獲取管理fragment的管理者對象;
 50         FragmentTransaction fragmentTransaction = fm.beginTransaction(); //開始事務的處理:與commit是一對;
 51         fragmentTransaction.replace(R.id.content_layout, mHomeFragment); //切換fragment
 52         fragmentTransaction.commit(); //執行切換的動做;
 53     }
 54 
 55     private void startAllService() {
 56         Intent intent = new Intent(this, UpdateProductService.class);
 57         startService(intent);
 58     }
 59 
 60     private void initView() {
 61         mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
 62         mHomeLayout.setOnClickListener(this);
 63         mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
 64         mPondLayout.setOnClickListener(this);
 65         mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
 66         mMessageLayout.setOnClickListener(this);
 67         mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
 68         mMineLayout.setOnClickListener(this);
 69 
 70         mHomeView = (TextView) findViewById(R.id.home_image_view);
 71         mPondView = (TextView) findViewById(R.id.fish_image_view);
 72         mMessageView = (TextView) findViewById(R.id.message_image_view);
 73         mMineView = (TextView) findViewById(R.id.mine_image_view);
 74         mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
 75     }
 76 
 77     @Override
 78     protected void onDestroy() {
 79         super.onDestroy();
 80     }
 81 
 82     /**
 83      * 隱藏fragment的共有方法
 84      * @param fragment
 85      * @param ft
 86      */
 87     private void hideFragment(Fragment fragment, FragmentTransaction ft) {
 88         if (fragment != null) {
 89             ft.hide(fragment);
 90         }
 91     }
 92 
 93     @Override
 94     public void onClick(View v) {
 95         FragmentTransaction fragmentTransaction = fm.beginTransaction();
 96         switch (v.getId()) {
 97             //點擊切換homeFragment
 98             case R.id.home_layout_view:
 99                 changeStatusBarColor(R.color.color_fed952);
100                 mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
101                 mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
102                 mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
103                 mMineView.setBackgroundResource(R.drawable.comui_tab_person);
104                 //在顯示homeFragment的時候,須要隱藏其餘的fragment;
105                 hideFragment(mCommonFragmentOne, fragmentTransaction);
106                 hideFragment(mMessageFragment, fragmentTransaction);
107                 hideFragment(mMineFragment, fragmentTransaction);
108                 //顯示homeFragment,不爲空則顯示;
109                 if (mHomeFragment == null) {
110                     mHomeFragment = new HomeFragment();
111                     fragmentTransaction.add(R.id.content_layout, mHomeFragment);
112                 } else {
113                     mCurrent = mHomeFragment;
114                     fragmentTransaction.show(mHomeFragment);
115                 }
116                 break;
117             //點擊切換消息fragment
118             case R.id.message_layout_view:
119                 changeStatusBarColor(R.color.color_e3e3e3);
120                 mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected);
121                 mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
122                 mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
123                 mMineView.setBackgroundResource(R.drawable.comui_tab_person);
124 
125                 hideFragment(mCommonFragmentOne, fragmentTransaction);
126                 hideFragment(mHomeFragment, fragmentTransaction);
127                 hideFragment(mMineFragment, fragmentTransaction);
128                 if (mMessageFragment == null) {
129                     mMessageFragment = new MessageFragment();
130                     fragmentTransaction.add(R.id.content_layout, mMessageFragment);
131                 } else {
132                     mCurrent = mMessageFragment;
133                     fragmentTransaction.show(mMessageFragment);
134                 }
135                 break;
136             //點擊切換「個人」fragment
137             case R.id.mine_layout_view:
138                 changeStatusBarColor(R.color.white);
139                 mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected);
140                 mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
141                 mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
142                 mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
143                 hideFragment(mCommonFragmentOne, fragmentTransaction);
144                 hideFragment(mMessageFragment, fragmentTransaction);
145                 hideFragment(mHomeFragment, fragmentTransaction);
146                 if (mMineFragment == null) {
147                     mMineFragment = new MineFragment();
148                     fragmentTransaction.add(R.id.content_layout, mMineFragment);
149                 } else {
150                     mCurrent = mMineFragment;
151                     fragmentTransaction.show(mMineFragment);
152                 }
153                 break;
154         }
155 
156         fragmentTransaction.commit();
157     }
158 }

 【效果】點擊三個按鈕,能夠切換不一樣的fragment;

 3.【總結】

【問題1】fragment的幾種切換方式?

【答】有dettach  attach的   
  有replcace的
 有hide  show的
  我以爲現實隱藏的方式最好
 由於上面的兩種頻繁的建立銷燬 消耗性能  且不能保留狀態

【答案】show hide, 減小了view重建的時間

相關文章
相關標籤/搜索