SlideShare a Scribd company logo
Android development
patterns and tips
Ting Cheng
USING LAZY LOADING AND
AVOIDING REPLICATION
• <include />
• <merge />
• <ViewStub />
<merge />
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textColor="@android:color/white"
android:textSize="32sp"
android:gravity="center"
android:layout_gravity="bottom"
android:background="#90000000"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
<ViewStub />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
</LinearLayout>
<ViewStub
android:id="@+id/main2_company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/company" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="company"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
public class Main extends Activity {
private final static String TAG = "Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
ViewStub view = (ViewStub) findViewById(R.id.main2_company);
view.inflate();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
• ViewStub 只能 inflate 一次。
• ViewStub 是用來 inflate layout view,而不是
View。
Logger
• public static boolean isLoggable (String tag, int level)
• setprop log.tag.<YOUR_LOG_TAG> <LEVEL>
• VERBOSE, DEBUG, INFO, WARN, ERROR,
ASSERT, or SUPPRESS
DEMO
The Model-View-Presenter
pattern
差異
• MVC
• View 可以與 Model 直接互動。
• Controller 是基於行為,並且可被多個 View 共享。
• MVP
• View 不直接與 Model 互動。
• Presenter 和 View 互動是通過 interface 來互動,方便進行單元測
試。
• 通常 View 和 Presenter 是一對一的,但複雜 View 可能有多個
Presenter 來處理邏輯。
四要素
• View - 負責顯示 UI 元素,與用戶進行互動。
• View Interface - 需要 View 實現的界面,用來和
Presenter 互動,降低耦合,方便進行單元測試。
• Model - 資料的操作,或者功能的細節。
• Presenter - 作為 View 和 Model 的互動,處理與
用戶的互動邏輯。
DEMO

More Related Content

Similar to Android development patterns and tips

淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践
Du Yamin
 
淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践jay li
 
Html5开发android应用程序概述
Html5开发android应用程序概述Html5开发android应用程序概述
Html5开发android应用程序概述
kevin_yanggl
 
Android应用开发 - 沈大海
Android应用开发 - 沈大海Android应用开发 - 沈大海
Android应用开发 - 沈大海
Shaoning Pan
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Design
jay li
 
Windows Mobile Widget 開發
Windows Mobile Widget 開發Windows Mobile Widget 開發
Windows Mobile Widget 開發Chui-Wen Chiu
 
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
compassdesign
 
S(P)Aas081008
S(P)Aas081008S(P)Aas081008
S(P)Aas081008yiming he
 
HTML5概览
HTML5概览HTML5概览
HTML5概览Adam Lu
 
Html5和css3入门
Html5和css3入门Html5和css3入门
Html5和css3入门
Xiujun Ma
 
Dive into Responsive Web Design
Dive into Responsive Web DesignDive into Responsive Web Design
Dive into Responsive Web Design
cncuckoo
 
Denver CMS Expo Creating CSS template
Denver CMS Expo Creating CSS templateDenver CMS Expo Creating CSS template
Denver CMS Expo Creating CSS templatecompassdesign
 
Creating CSS Template with Barrie North
Creating CSS Template with Barrie NorthCreating CSS Template with Barrie North
Creating CSS Template with Barrie North
John Coonen
 
Google App Engine Devfest 200810 External
Google App Engine Devfest 200810 ExternalGoogle App Engine Devfest 200810 External
Google App Engine Devfest 200810 Externaljunyu
 
Response Web Design
Response Web DesignResponse Web Design
Response Web Design
Sichu Zhang
 
揭秘Html5和Css3
揭秘Html5和Css3揭秘Html5和Css3
揭秘Html5和Css3Adam Lu
 
Html5css3 go.yeefe.com
Html5css3 go.yeefe.comHtml5css3 go.yeefe.com
Html5css3 go.yeefe.comtellyeefe
 
揭秘Html5和Css3 ---- 鲁超伍
揭秘Html5和Css3 ---- 鲁超伍揭秘Html5和Css3 ---- 鲁超伍
揭秘Html5和Css3 ---- 鲁超伍
裕波 周
 

Similar to Android development patterns and tips (20)

淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践
 
淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践
 
Html5开发android应用程序概述
Html5开发android应用程序概述Html5开发android应用程序概述
Html5开发android应用程序概述
 
Android应用开发 - 沈大海
Android应用开发 - 沈大海Android应用开发 - 沈大海
Android应用开发 - 沈大海
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Design
 
Windows Mobile Widget 開發
Windows Mobile Widget 開發Windows Mobile Widget 開發
Windows Mobile Widget 開發
 
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
Chicago EXPO Creating a Pure CSS Template in Joomla 1.5
 
S(P)Aas081008
S(P)Aas081008S(P)Aas081008
S(P)Aas081008
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
 
Html5和css3入门
Html5和css3入门Html5和css3入门
Html5和css3入门
 
Dive into Responsive Web Design
Dive into Responsive Web DesignDive into Responsive Web Design
Dive into Responsive Web Design
 
Hello ipad
Hello ipadHello ipad
Hello ipad
 
Denver CMS Expo Creating CSS template
Denver CMS Expo Creating CSS templateDenver CMS Expo Creating CSS template
Denver CMS Expo Creating CSS template
 
Creating CSS Template with Barrie North
Creating CSS Template with Barrie NorthCreating CSS Template with Barrie North
Creating CSS Template with Barrie North
 
Google App Engine Devfest 200810 External
Google App Engine Devfest 200810 ExternalGoogle App Engine Devfest 200810 External
Google App Engine Devfest 200810 External
 
Yid chart
Yid chartYid chart
Yid chart
 
Response Web Design
Response Web DesignResponse Web Design
Response Web Design
 
揭秘Html5和Css3
揭秘Html5和Css3揭秘Html5和Css3
揭秘Html5和Css3
 
Html5css3 go.yeefe.com
Html5css3 go.yeefe.comHtml5css3 go.yeefe.com
Html5css3 go.yeefe.com
 
揭秘Html5和Css3 ---- 鲁超伍
揭秘Html5和Css3 ---- 鲁超伍揭秘Html5和Css3 ---- 鲁超伍
揭秘Html5和Css3 ---- 鲁超伍
 

Android development patterns and tips

  • 2. USING LAZY LOADING AND AVOIDING REPLICATION • <include /> • <merge /> • <ViewStub />
  • 4. <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Save" android:textColor="@android:color/white" android:textSize="32sp" android:gravity="center" android:layout_gravity="bottom" android:background="#90000000" android:textAppearance="?android:attr/textAppearanceLarge" /> </FrameLayout>
  • 5.
  • 6.
  • 8. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="username" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" > </EditText> </LinearLayout> <ViewStub android:id="@+id/main2_company" android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/company" /> </LinearLayout>
  • 9. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="company" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout>
  • 10.
  • 11. public class Main extends Activity { private final static String TAG = "Main"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); ViewStub view = (ViewStub) findViewById(R.id.main2_company); view.inflate(); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); } }
  • 12.
  • 13. • ViewStub 只能 inflate 一次。 • ViewStub 是用來 inflate layout view,而不是 View。
  • 14. Logger • public static boolean isLoggable (String tag, int level) • setprop log.tag.<YOUR_LOG_TAG> <LEVEL> • VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS
  • 15. DEMO
  • 17. 差異 • MVC • View 可以與 Model 直接互動。 • Controller 是基於行為,並且可被多個 View 共享。 • MVP • View 不直接與 Model 互動。 • Presenter 和 View 互動是通過 interface 來互動,方便進行單元測 試。 • 通常 View 和 Presenter 是一對一的,但複雜 View 可能有多個 Presenter 來處理邏輯。
  • 18. 四要素 • View - 負責顯示 UI 元素,與用戶進行互動。 • View Interface - 需要 View 實現的界面,用來和 Presenter 互動,降低耦合,方便進行單元測試。 • Model - 資料的操作,或者功能的細節。 • Presenter - 作為 View 和 Model 的互動,處理與 用戶的互動邏輯。
  • 19. DEMO