19
Android development patterns and tips Ting Cheng

Android development patterns and tips

Embed Size (px)

Citation preview

Page 1: Android development patterns and tips

Android development

patterns and tipsTing Cheng

Page 2: Android development patterns and tips

USING LAZY LOADING AND

AVOIDING REPLICATION

• <include />

• <merge />

• <ViewStub />

Page 3: Android development patterns and tips

<merge />

Page 4: Android development patterns and tips

<?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>

Page 5: Android development patterns and tips
Page 6: Android development patterns and tips
Page 7: Android development patterns and tips

<ViewStub />

Page 8: Android development patterns and tips

<?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>

Page 9: Android development patterns and tips

<?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>

Page 10: Android development patterns and tips
Page 11: Android development patterns and tips

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();

}

}

Page 12: Android development patterns and tips
Page 13: Android development patterns and tips

• ViewStub 只能 inflate 一次。

• ViewStub 是用來 inflate layout view,而不是View。

Page 14: Android development patterns and tips

Logger

• public static boolean isLoggable (String tag, int level)

• setprop log.tag.<YOUR_LOG_TAG> <LEVEL>

• VERBOSE, DEBUG, INFO, WARN, ERROR,

ASSERT, or SUPPRESS

Page 15: Android development patterns and tips

DEMO

Page 16: Android development patterns and tips

The Model-View-Presenter

pattern

Page 17: Android development patterns and tips

差異

• MVC

• View 可以與 Model 直接互動。

• Controller 是基於行為,並且可被多個 View 共享。

• MVP

• View 不直接與 Model 互動。

• Presenter 和 View 互動是通過 interface 來互動,方便進行單元測試。

• 通常 View 和 Presenter 是一對一的,但複雜 View 可能有多個Presenter 來處理邏輯。

Page 18: Android development patterns and tips

四要素

• View -負責顯示 UI 元素,與用戶進行互動。

• View Interface -需要 View 實現的界面,用來和Presenter 互動,降低耦合,方便進行單元測試。

• Model -資料的操作,或者功能的細節。

• Presenter -作為 View 和 Model 的互動,處理與用戶的互動邏輯。

Page 19: Android development patterns and tips

DEMO