TRY ANDROID DEV

Android アプリ開発のコーディングネタ。あとエンジニアとしての活動

GW:全力でTODOアプリを作ってみる(2日目)

背景

  • GWなのでアウトプットに専念してみる
  • 一旦基本のTODOアプリを作ったらどんな風になるのか試してみる
  • 子供が寝た隙に毎日1時間程度の稼働でどこまでいけるか
  • 子供が高熱を出して早速3日ストップした。。。

Navigationを用意する

まずはres/navigation フォルダを作成してnavigation.xmlを作成する。

<?xml version="1.0" encoding="utf-8"?>
<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"

    android:id="@+id/navigation"
    app:startDestination="@id/todolist">

    <fragment
        android:id="@+id/todolist"
        android:name="todo.view.TodoListFragment"
        tools:layout="@layout/fragment_todolist" />
</navigation>

あとはActivityのlayoutファイルにnavigationを設定する。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

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

</androidx.constraintlayout.widget.ConstraintLayout>

fragmentのlayoutファイルにはRecyclerViewを設定しておく。 tools:listitemをRecyclerViewに設定すると、Designタブで見れるので良い。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:spanCount="1"
        tools:itemCount="30"
        tools:listitem="@layout/item_todo"
        tools:orientation="vertical" />

</LinearLayout>

RecyclerViewのtoolsについては以下のQiita記事にまとめた。

qiita.com

感想

  • なかなか時間が取れずやきもきする。。