Actionbar and fragment

Post on 10-May-2015

18.959 views 1 download

Transcript of Actionbar and fragment

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

ActionBar

애플리케이션 아이콘

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

뷰 컨트롤

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

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

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

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

애플리케이션 아이콘

getActionBar().setDisplayAsUpEnable(true|false)

getActionBar().setDisplayUseLogoEnabled(true|false)

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

MenuItem 값은 action.R.id.home

뷰 컨트롤

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)

액션 버튼

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

액션 버튼

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>

@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); }}

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

문맥 액션바

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

startActionMode( mCallback );

Action Provider

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

Froyo? GingerBread?

http://actionbarsherlock.com

Jake Wharton

Fragment

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

</LinearLayout>

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

</LinearLayout>

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

</LinearLayout>

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

</LinearLayout>

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

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

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

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

Activity를 중첩하면 어때?

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

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

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

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

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

Fragment

독립적인 UI 단위

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

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>

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를 주로 사용.

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

타블렛 + 폰<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 존재 확인

타블렛 + 폰<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 존재 확인

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

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

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

DialogFragment

ListFragment

PreferenceFragment

WebViewFragment

YouTubePlayerFragment

MapFragment

Android UI