Actionbar and fragment

32
ActionBar & Fragment Leonardo YongUk Kim http://dalinaum-kr.tumblr.com

Transcript of Actionbar and fragment

Page 1: Actionbar and fragment

ActionBar & FragmentLeonardo YongUk Kim http://dalinaum-kr.tumblr.com

Page 2: Actionbar and fragment

ActionBar

Page 3: Actionbar and fragment

애플리케이션 아이콘

최상위 화면이 아니면 캐럿(<)을 포함해야 함.

뷰 컨트롤

여러 뷰를 사용할 때. 스피너나 탭을 제공해야.

액션 버튼(3)과 액션 오버플로우(4)

메뉴 버튼은 사용하지 않음.

부족한 항목은 오버플로우를 통해 표시되어야 함.

Page 4: Actionbar and fragment
Page 5: Actionbar and fragment

애플리케이션 아이콘

getActionBar().setDisplayAsUpEnable(true|false)

getActionBar().setDisplayUseLogoEnabled(true|false)

UP 선택시 onOptionsItemSelected(MenuItem)이 호출.

MenuItem 값은 action.R.id.home

Page 6: Actionbar and fragment

뷰 컨트롤

getActionBar().setNavigation(ActionBar.NAVIGATION_MODE_LIST)

setListNavigationCallbacks(SpinnerAdapter,

OnNavigationListener)

getActionBar().setNavigation(ActionBar.NAVIGATION_MODE_TAB)

tab = getActionBar().newTab().setText(“TAB”)

.setTabListener(new MyListener(Fragment))

getActionBar().addTab(tab)

Page 7: Actionbar and fragment

액션 버튼

onCreateOptionsMenu(Menu) { getMenuInflater().inflate(RID, Menu)}

Page 8: Actionbar and fragment

액션 버튼

onCreateOptionsMenu(Menu) { getMenuInflater().inflate(ResourceID, Menu)}

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /></menu>

Page 9: Actionbar and fragment

@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go home Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); }}

Page 10: Actionbar and fragment

문맥 액션바 (CAB: Contextual Action Bar)

Page 11: Actionbar and fragment

문맥 액션바

private ActionMode.Callback mCallback = new ActionMode.Callback() {}

startActionMode( mCallback );

Page 12: Actionbar and fragment

Action Provider

<item android:id="@+id/menu_share" android:title="@string/share" android:showAsAction="ifRoom" android:actionProviderClass="android.widget.ShareActionProvider" /> ...

Page 13: Actionbar and fragment

Froyo? GingerBread?

http://actionbarsherlock.com

Jake Wharton

Page 14: Actionbar and fragment

Fragment

Page 15: Actionbar and fragment

<LinearLayout><include layout=”...><include layout=”...>

</LinearLayout>

<LinearLayout><include layout=”...><include layout=”...>

</LinearLayout>

Page 16: Actionbar and fragment

<LinearLayout><include layout=”...><include layout=”...>

</LinearLayout>

<LinearLayout><include layout=”...><include layout=”...>

</LinearLayout>

중첩된 요소를 관리할 방법이 없었습니다.

Page 17: Actionbar and fragment

ActivityGroup (eg. TabActivity)는 Activity를 포함시킬 수 있었습니다. (현재는 deprecated)

View 모델로는 상황에 따른 관리가 어렵다.

Activity는 중첩을 고려하지 않아 여러 문제가 존재.

Activity를 중첩하면 어때?

Page 18: Actionbar and fragment

계층 레이아웃이 아니면 문제 없는 것 아네요?

Page 19: Actionbar and fragment

회전 등으로 액티비티가 사라지면 프로그래머가 알아서 해야 합니다.

Page 20: Actionbar and fragment

액티비티 간에 정보 전달할 방법은 인텐트와 onRetainNonConfigurationInstance (deprecated)

전역적인 정보 공유 방법이 부재.

처리하던 작업이나 외부에서 오는 노티를 액티비티가 받기 어려움.

Page 21: Actionbar and fragment

Fragment

독립적인 UI 단위

Page 22: Actionbar and fragment
Page 23: Actionbar and fragment

생명 주기를 통한시스템에 의한 관리

Page 24: Actionbar and fragment

fragment에 class를 정적으로 넣어 생성하는 방법

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment" android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>

Page 25: Actionbar and fragment

DetailsFragment details = (DetailsFragment)                    getFragmentManager().findFragmentById(R.id.details);            if (details == null || details.getShownIndex() != index) {                // Make new fragment to show this selection.                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing                // fragment with this one inside the frame.                FragmentTransaction ft                        = getFragmentManager().beginTransaction();                ft.replace(R.id.details, details);                ft.setTransition(                        FragmentTransaction.TRANSIT_FRAGMENT_FADE);                ft.commit();            }

fragment를 동적으로 생성하거나 교체하는 방법

한번에 여러 fragment가 교체될 수 있고 백스택에 의해 관리됨. 그렇기 때문에 트랜잭션을 만들어야 함.

add와 replace를 주로 사용.

Page 26: Actionbar and fragment

한번의 트랜잭션 동안 두개의 프래그먼트를 교체.

Page 27: Actionbar and fragment

타블렛 + 폰<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.TitlesFragment"            android:id="@+id/titles" android:layout_weight="1"            android:layout_width="0px"            android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"            android:layout_width="0px"            android:layout_height="match_parent" />    </LinearLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">    <fragment class="com.example.android.apis.app.TitlesFragment"            android:id="@+id/titles"            android:layout_width="match_parent"            android:layout_height="match_parent" /></FrameLayout>

두개의 layout

DetailsFragment details = (DetailsFragment)                    getFragmentManager().findFragmentById(R.id.details);            if (details == null || details.getShownIndex() != index) {

런타임으로 서브 Fragment 존재 확인

Page 28: Actionbar and fragment

타블렛 + 폰<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.TitlesFragment"            android:id="@+id/titles" android:layout_weight="1"            android:layout_width="0px"            android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"            android:layout_width="0px"            android:layout_height="match_parent" />    </LinearLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">    <fragment class="com.example.android.apis.app.TitlesFragment"            android:id="@+id/titles"            android:layout_width="match_parent"            android:layout_height="match_parent" /></FrameLayout>

두개의 layout

DetailsFragment details = (DetailsFragment)                    getFragmentManager().findFragmentById(R.id.details);            if (details == null || details.getShownIndex() != index) {

런타임으로 서브 Fragment 존재 확인

뷰 컴포넌트를 재사용가능하고시스템이 리소스 관리를 도와주는 것일 뿐.

프로그래머는 코딩해야 합니다.

Page 29: Actionbar and fragment

기본 콤포넌트와 구글 API는 점차 Fragment 위주로

DialogFragment

ListFragment

PreferenceFragment

WebViewFragment

YouTubePlayerFragment

MapFragment

Page 30: Actionbar and fragment

Android UI

Page 31: Actionbar and fragment
Page 32: Actionbar and fragment