-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
iOrchid
committed
Mar 23, 2024
1 parent
392d748
commit 91ab49b
Showing
13 changed files
with
239 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
jetpack/src/main/java/org/zhiwei/jetpack/components/ui/JetpackFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.zhiwei.jetpack.components.ui | ||
|
||
import android.icu.math.BigDecimal | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import org.zhiwei.jetpack.components.R | ||
|
||
/** | ||
* 这部分主要演示除dataBinding外的,主要的几个jetpack组件的用法; | ||
* 1. liveData、lifecycle、viewModel,room,paging,work,navigation | ||
* 2. 其实在JetpackActivity中就已经使用了navigation结合bottomNavigationView的简单用法 | ||
*/ | ||
class JetpackFragment : Fragment() { | ||
//fragment-ktx提供的扩展函数,便于获取viewModel的实例;该方式获取的vm,不同的fragment对象获取后的vm,不是同一个实例。 | ||
private val vm: JetpackViewModel by viewModels() | ||
|
||
//fragment-ktx提供的扩展函数,便于获取该fragment依附的activity的viewModel的实例。 | ||
// 该方式获取的vm,如果是同一个activity下不同的fragment获取vm,对象是同一个。 | ||
// private val vm2: JetpackViewModel by activityViewModels() | ||
|
||
private val tvLive: TextView by lazy { requireView().findViewById(R.id.tv_live_ret_jetpack) } | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View? { | ||
//这里注意,第三个参数一定要设置false,否则报错 | ||
return inflater.inflate(R.layout.fragment_jetpack, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
testLiveData() | ||
} | ||
|
||
private val TAG = "JetpackFragment" | ||
private fun testLiveData() { | ||
//模拟生成数据 | ||
vm.startSendScore() | ||
//观察live的数据变化,不要关联生命周期的lifecycleOwner,observeForever不需要。 | ||
//viewLifecycleOwner是fragment的,activity就是自身。 | ||
vm.liveScore.observe(viewLifecycleOwner) { | ||
//保留小数点2位 | ||
val num = BigDecimal(it).setScale(2, BigDecimal.ROUND_UP) | ||
Log.d(TAG, "testLiveData:👀到$it ,保留2位:$num") | ||
tvLive.text = "文本$num" | ||
} | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
jetpack/src/main/java/org/zhiwei/jetpack/components/ui/JetpackViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.zhiwei.jetpack.components.ui | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlin.math.nextDown | ||
import kotlin.random.Random | ||
|
||
/** | ||
* 作用于JetpackActivity和JetpackFragment的viewModel; | ||
* viewModel作为数据管理层,可以给多个fragment共用。 | ||
* | ||
*/ | ||
class JetpackViewModel : ViewModel() { | ||
//一般使用ViewModel作为基类即可,另有AndroidViewModel,内含application参数,根据业务场景来选择使用与否。 | ||
|
||
//region liveData | ||
private val _liveStudentScore = | ||
MutableLiveData<Double>()//一般mutable的都是可修改的,开发习惯,可修改额私有,对外提供不可变的。 | ||
val liveScore: LiveData<Double> = _liveStudentScore | ||
|
||
//endregion | ||
|
||
internal fun startSendScore() { | ||
viewModelScope.launch { | ||
repeat(20) { | ||
delay(500) | ||
_liveStudentScore.postValue(Random.nextDouble(20.0, 100.0).nextDown()) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
jetpack/src/main/java/org/zhiwei/jetpack/components/ui/jetpack笔记速览.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#### Jetpack笔记速览 | ||
|
||
##### 一、前言 | ||
|
||
> [Jetpack](https://developer.android.google.cn/jetpack?hl=zh-cn) | ||
> 是Android官方推出的一些列组件库,最早`databinding` | ||
> 也属于官方推荐的工具写法,但是由于其影响编译性能,且`xml` | ||
> 中编写出错的时候IDE不易准确检测和报错,逐步不再使用。且`compose` | ||
> 更为高效的UI编写方式,所以该项目内`dataBinding`有独立module作为代码示例,并有详细注释说明,便于学习。 | ||
> | ||
> 如果不是接手老项目需要,可以跳过,直接学习compose;如果是老项目有databinding,则可以学习了解即可。 | ||
Jetpack的其他组件则都会共用在同一个项目module内,同时也是使用MVVM的架构设计模式,来简单实现一个组合使用各个库的示例。 | ||
|
||
<font color=orange> | ||
以下笔记仅用于初步了解库的使用和简单构成,不太深入基本原理。先知其然,然后自然而然你会探索其所以然。</font> | ||
|
||
可以在`IDE`的project模式下,`External Libraries`找到想看的库的编译源码。 | ||
|
||
##### 二、Lifecycle | ||
|
||
> 简言之,Lifecycle就是观察者模式,用以感知UI元素的生命周期的。Android中Activity/Fragment级别的实现。 | ||
简要理解`Lifecycle`库就核心三个类文件:`Lifecycle`、`LifecycleOwner`、`LifecycleObserver` | ||
|
||
1. `Lifecycle`就是表述生命周期的,抽象类,提供观察者的注册/注销。内有生命周期的不同状态`State` | ||
和`Event`。 | ||
2. `LifecycleOwner`就是上面`lifecycle`的持有者,Android中是`componentActivity/Fragment` | ||
类实现的。两个`componentActivity`类,一般使用`andriodx.activity`包下的,因为这个实现了`viewModel` | ||
的结合。开发中多用`AppcompatActivity来作为基类`,直接使用`Activity`作为基类就没有这个实现。 | ||
3. `LifecycleObserver`和子类`LifecycleEventObsever`就可以添加到上面的`lifecycle` | ||
注册观察,就能感知到生命周期变化的回调。具体你不实现,可参照源码,`LifecycleController`等分析。 | ||
|
||
##### 三、LiveData | ||
|
||
> 又一个观察者模式的实现,数据变化时候可以得到及时的通知。两个核心库`lifecycle-livedata-core` | ||
> 和`lifecycle-livedata` | ||
1. `core`库就三个文件:`Observer`接口,`LiveData`抽象类和`MutableLiveData` | ||
的类。LiveData内有观察者的注册/注销,及数据变化的管理和通知等逻辑。 | ||
2. `livedata`库有`MediatorLiveData`,`Transformations`和一些**协程**相关的实现类,扩展函数等。 | ||
|
||
```kotlin | ||
``` | ||
|
||
Worker | ||
任务的执行者,是一个抽象类,需要继承它实现要执行的任务。 | ||
|
||
WorkRequest | ||
指定让哪个 Worker 执行任务,指定执行的环境,执行的顺序等。 | ||
要使用它的子类 OneTimeWorkRequest 或 PeriodicWorkRequest。 | ||
|
||
WorkManager | ||
管理任务请求和任务队列,发起的 WorkRequest 会进入它的任务队列。 | ||
|
||
WorkStatus | ||
包含有任务的状态和任务的信息,以 LiveData 的形式提供给观察者。 | ||
|
||
WorkManager适用于那些即使应用程序退出,系统也能够保证这个任务正常运行的场景,比如将应用程序数据上传到服务器。 | ||
它不适用于应用进程内的后台工作,如果应用进程消失,就可以安全地终止,对于这种情况,推荐你使用线程池 |
15 changes: 0 additions & 15 deletions
15
jetpack/src/main/java/org/zhiwei/jetpack/components/ui/work.txt
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...hiwei/jetpack/components/ui/MyWorker.java → .../jetpack/components/ui/work/MyWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.material.textview.MaterialTextView | ||
android:id="@+id/tv_title_kotlin" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="20dp" | ||
android:fontFamily="@font/hongkong" | ||
android:text="Jetpack學習" | ||
android:textSize="22sp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_live_ret_jetpack" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="此处显式liveData的变化值" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/tv_title_kotlin" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ class ExampleUnitTest { | |
fun addition_isCorrect() { | ||
assertEquals(4, 2 + 2) | ||
} | ||
|
||
} |