『Android Kotlin Fundamentals』をやった(3)

Android Kotlin Fundamentals 02.4:Data binding basics

an object that contains a reference to each view. This object, called a Binding object,

Bindingオブジェクトとは各Viewへの参照を包含するオブジェクト…φ(・ω・` )メモメモ…

To use data binding, you need to enable data binding in your Gradle file, as it's not enabled by default.
To work with data binding, you need to wrap your XML layout with a <layout> tag.
The namespace declarations for a layout must be in the outermost tag.

build.gradle(:app)でdatabindingを有効にして、
<layout>タグで全体を囲って、xmlnsを最外である<layout>タグ内に移動。

あれ…
なんかアプリが繰り返し停止するとか言ってエラーが(ノ∀`)
kaptを追加していないのがいけないかと思い、pluginのところに

id 'kotlin-kapt'

を追加したが、関係なかった模様。

    buildFeatures {
        dataBinding true
    }

ではなく、

    dataBinding {
        enabled true
    }

にしてみたけど、
別に関係なく…まあそれはそうかw

あー、中身は空でもいいけど<data></data>を追加しないと駄目ってことなのかな?
<layout>タグで囲んだ後は<data>タグも一緒に追加しないと起動でこけるみたいだな、少なからず、うちの今の環境だと。

おお、エラーになっていたMainActivity.ktの
import com.example.aboutme.databinding.ActivityMainBinding
が自動追加された。サンプルと違って、うちのコードのパッケージにandroidがないな…
まあそこはどうでもいいかw

import androidx.databinding.DataBindingUtil
を追加するとエラーになって
import android.databinding.DataBindingUtilが追加されるな…
jetpackから移動したんだろうか(´・ω・`)?

Step 4: Use the binding object to replace all calls to findViewById()

When the binding object is created, the compiler generates the names of the views in the binding object from the IDs of the views in the layout, converting them to camel case. So, for example, done_button is doneButton in the binding object, nickname_edit becomes becomes nicknameEdit, and nickname_text becomes nicknameText.

ほーん、なるほど…( ゜σ・゚)ホジホジ
ってbecomesが二回出てきてるなw

findViewById(id)で指定してたら、自動的にbindingオブジェクトを追加するか、同じメソッド内にあるbindingオブジェクトを選択して、~.キャメルケース化したidとかに変更する機能はないんかな。それは一括でリファクタリング出来る機能として存在した方がいいのかな…

完成形の方にupdateNicknameがないな…
いずれ削除するのかな?

thorough 徹底的に 充分な, 十分な, 念入りな
一瞬、throughかと思ったら、こんな英単語があるんだな…


4. Task: Use data binding to display data

Step 2: Add data to the layout

この段階でタグを挿入するのか…
他の環境だと上記のエラーは起きないのであろうか?
何はともあれThis is where you will connect the view with the data.ということらしい。


Android Kotlin Fundamentals:Create a fragment

2. App overview

The top of the app displays a view called the *app bar (*or action bar) which shows the app's name.

この*はなんだろうか…
もしかして強調表示とかの制御文字の一部?


4. Task: Add a fragment

Step 1: Add a Fragment class

Create a binding object

これ項番5でplaceholder_layout.xmlを削除するけど、これってなんなんだろう?

Step 2: Add the new fragment to the main layout file

今まで、ずっとtext tabと言ってたのに、ここでようやくCode tabという表記になった。


Android Kotlin Fundamentals:03.2 Define navigation paths

5. Task: Add fragments to the navigation graph

Step 1: Add two fragments to the navigation graph and connect them with an action

placeholder_layout.xmlを削除し忘れていた為かなんか知らないけど、titleFragmentが出てこなくて悩んだ(ノ∀`)

Step 2: Add a click handler to the Play button

最後のopens.と画像の間に改行が欲しいところか。


7. Task: Change the Back button's destination

Step 1: Set the pop behavior for the navigation actions

あれ? popUpTo属性がよくわからないと思ってぐぐったら、
コードラボを翻訳しているサイトを見つけた(ノ∀`)

まあしばらくは原文サイトで読み続けようかな。

項番4の
Select the action for navigating from the gameFragment to the gameWonFragment. Again, set popUpTo to gameFragment in the Attributes pane and select the popUpToInclusive checkbox.
ってあるけど、画像だとgameWonFragmentになっているような気がするけど、これはなんなんだろうか…

Step 2: Add more navigation actions and add onClick handlers

val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this,navController)

はbindingの後に書かないとまたエラーが発生して起動しなくなった。

Step 2: Add the options-menu resource

追加したmenu itemにはidが入っていないのがデフォ?
取り敢えずaboutFragmentにしたけれど。


10. Task: Add the navigation drawer

Step 3: Create the drawer menu and the drawer layout

Note: If you use the same ID for the menu item as for the destination Fragment, you don't need to write any code at all to implement the onClick listener!

へぇ(・∀・)
随分と簡略化されてるんだなぁ。


Android Kotlin Fundamentals: 03.3 Start an external Activity

3. Task: Set up and use the Safe Args plugin

An Android Bundle is a key-value store.

Bundleはキーバリューペアを格納するオブジェクトと…φ(・ω・` )メモメモ…

Android's Navigation Architecture Component includes a feature called Safe Args. Safe Args is a Gradle plugin that generates code and classes that help detect errors at compile-time that might not otherwise be surfaced until the app runs.

Safe Argsはアプリ実行時まで表面化しないかもしれないエラーをコンパイル時に発見する手助けをするコードとクラスを生成するGradleプラグイン?

Step 3: Add a NavDirection class to the game Fragment

GameFragmentDirectionsというか~Directions.javaファイルってASから見られるのかな?
なんか見えない気がする。エクスプローラで開くと表示されるんだけども…
\android-kotlin-fundamentals-starter-apps-master\AndroidTrivia-Starter\app\build\generated\source\navigation-args\debug\com\example\android\navigation

import androidx.annotation.NonNull;
import androidx.navigation.ActionOnlyNavDirections;
import androidx.navigation.NavDirections;

public class GameFragmentDirections {
  private GameFragmentDirections() {
  }

  @NonNull
  public static NavDirections actionGameFragmentToGameOverFragment() {
    return new ActionOnlyNavDirections(R.id.action_gameFragment_to_gameOverFragment);
  }

  @NonNull
  public static NavDirections actionGameFragmentToGameWonFragment() {
    return new ActionOnlyNavDirections(R.id.action_gameFragment_to_gameWonFragment);
  }
}

NavDirectionsはインターフェースなのか。

ActionOnlyNavDirectionsはその継承クラスでnavの矢印のidを保持して、getArguments()は常に新しいBundleを返す…?

Android の各種リソースの ID は int 型で管理されています。 この値を変数に格納したり、メソッドのパラメータとして受け取るように実装した場合、不正な int 値が渡されて実行時に落ちてしまうリスクがあります。

@IdRes R.id.xxx で参照するリソースID

へぇ(・∀・)

4. Task: Add and pass arguments

Step 2: Pass the arguments

ここでnumQuestionsとquestionIndexをパラメータとして追加したら何故か変な補完がされてエラーに。余計なvarやらany以降を削除したら、エラーは消えた。