Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Jetpack (再)入門 / DevFest Okayama 2019

star_zero
December 07, 2019

Android Jetpack (再)入門 / DevFest Okayama 2019

star_zero

December 07, 2019
Tweet

More Decks by star_zero

Other Decks in Programming

Transcript

  1. ViewModel class MainViewModel : ViewModel() { // } class MainActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val viewModel = ViewModelProviders .of(this).get(MainViewModel::class.java) } }
  2. LiveData class MainViewModel : ViewModel() { private val _data =

    MutableLiveData<String>() val data: LiveData<String> = _data fun fetchData() { val result = // データ取得 _data.value = result } }
  3. LiveData class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { viewModel.data.observe(this, Observer { // LiveDataが変更されるとココに通知される }) } }
  4. DataBinding class MainViewModel : ViewModel() { private val _data =

    MutableLiveData<String>() val data: LiveData<String> = _data fun fetchData() { val result = // データ取得 _data.value = result } }
  5. DataBinding <layout> <data> <variable name="viewModel" type="com...MainViewModel" /> </data> <ConstraintLayout> <TextView

    android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{viewModel.data}" /> </ConstraintLayout> </layout>
  6. DataBinding class MainActivity : AppCompatActivity() { private lateinit var binding:

    ActivityMainBinding private val viewModel by lazy { } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil .setContentView(this, R.layout.activity_main) binding.lifecycleOwner = this binding.viewModel = viewModel } }
  7. WorkManager class MyWorker( context: Context, params: WorkerParameters ): Worker(context, params)

    { override fun doWork(): Result { // タスクを実行 return Result.success() } }
  8. WorkManager val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .setRequiresCharging(true) .build() val workRequest

    = OneTimeWorkRequestBuilder<MyWorker>() .setConstraints(constraints) .build() WorkManager.getInstance(context).enqueue(workRequest)
  9. Room @Entity data class User( @PrimaryKey val id: Int, @ColumnInfo(name

    = "name") val name: String ) @Dao interface UserDao { @Query("SELECT * FROM user") fun getUsers(): List<User> @Insert fun insert(user: User) }
  10. Room val db = Room.databaseBuilder( requireContext(), AppDatabase::class.java, "my-db" ).build() val

    dao = db.userDao() // データ追加 dao.insert(User(1, "Kenji Abe")) // データ取得 val users = dao.getUsers()
  11. Paging @Dao interface UserDao { @Query("SELECT * FROM user ORDER

    BY id") fun getUsersPaging(): DataSource.Factory<Int, User> } class UserViewMode : ViewModel() { val users = userDao.getUsersPaging().toLiveData(10) }
  12. Paging class UsersAdapter : PagedListAdapter<User, ViewHolder>(DIFF_CALLBACK) { override fun onCreateViewHolder(/*

    */): ViewHolder { // ... return ViewHolder(view) } override fun onBindViewHolder((/* */) { holder.bind(getItem(position)) } }
  13. Paging class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val adapter = UsersAdapter() recycler.layoutManager = LinearLayoutManager(this) recycler.adapter = adapter viewModel.users.observe(this, Observer { adapter.submitList(it) }) } }
  14. CameraX val previewConfig = PreviewConfig.Builder().apply { // ... }.build() val

    preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { // ... } val imageCaptureConfig = ImageCaptureConfig.Builder() .apply { // ... }.build() val imageCapture = ImageCapture(imageCaptureConfig) CameraX.bindToLifecycle(this, preview, imageCapture)
  15. その他いろいろ • Benchmark ‣ パフォーマンス測定 • Biometric ‣ 生体認証 •

    ViewPager2 ‣ ViewPagerの新実装(RecyclerView) • Security ‣ 暗号化まわり
  16. KTX

  17. Jetpack Compose • Google I/O 2019で発表 • 宣言的UI ‣ Flutter,

    React, Vue.jsに似てる • Kotlinで書く • まだPreview版 • Android Studio 4.0で試せる