2019년 2월 27일 수요일

안드로이드 UI 네비게이션


- 네비게이션 레퍼런스
https://developer.android.com/topic/libraries/architecture/navigation.html?hl=ko


- Google Codelab
https://codelabs.developers.google.com/codelabs/android-navigation/#0





1. res/navigation/navi.xml 작성

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

</navigation>



- 2019.02.27 기준 app module dependencies 설정
implementation 'android.arch.navigation:navigation-fragment:1.0.0-beta02'

*** xml 문서 정의 후 해당 문서를 오픈하면 자동으로 dependencies 설정 팝업이 표시됩니다.







2. activity_main.xml 작성

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navi" />

</FrameLayout>







3. MainActivity 작성

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onSupportNavigateUp() {
        return Navigation.findNavController(this, R.id.host_fragment).navigateUp();
    }
}






4. Fragment 생성 후 navi.xml 수정
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/listFragment">


    <fragment
        android:id="@+id/listFragment"
        android:name="app.example.navigationui.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list" >
        <action
            android:id="@+id/action_listFragment_to_textFragment"
            app:destination="@id/textFragment"
            app:popUpTo="@+id/listFragment" />
    </fragment>
    <fragment
        android:id="@+id/textFragment"
        android:name="app.example.navigationui.TextFragment"
        android:label="fragment_text"
        tools:layout="@layout/fragment_text" >
        <argument
            android:name="text"
            app:argType="string"
            android:defaultValue="-" />
    </fragment>
</navigation>































5. 샘플코드
소스코드
APK


 

댓글 없음:

댓글 쓰기