-
-
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
Showing
9 changed files
with
318 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Android Architecture Components | ||
是谷歌在Google I/O 2017发布一套帮助开发者解决Android架构设计的方案。里面包含了两大块内容: | ||
生命周期相关的Lifecycle-aware Components | ||
数据库解决方案Room | ||
|
47 changes: 47 additions & 0 deletions
47
mvvm/src/main/java/org/zhiwei/jetpack/mvvm/model/UserModel.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.zhiwei.jetpack.mvvm.model; | ||
|
||
/** | ||
* 简单的model对象类,其实也可以使用接口形式,由具体子类实现 | ||
* Author: zhiwei. | ||
* Date: 2018/11/6 0006,19:28. | ||
*/ | ||
public class UserModel { | ||
|
||
private int age; | ||
private String name; | ||
private String city; | ||
|
||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UserModel{" + | ||
"age=" + age + | ||
", name='" + name + '\'' + | ||
", city='" + city + '\'' + | ||
'}'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
mvvm/src/main/java/org/zhiwei/jetpack/mvvm/persistent/DataRepository.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.zhiwei.jetpack.mvvm.persistent; | ||
|
||
|
||
import org.zhiwei.jetpack.mvvm.model.UserModel; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 数据代理类 | ||
* Author: zhiwei. | ||
* Date: 2018/11/6 0006,19:30. | ||
*/ | ||
public class DataRepository { | ||
|
||
private static DataRepository instance = new DataRepository(); | ||
private VirtualLocalData localData; | ||
|
||
private DataRepository() { | ||
localData = VirtualLocalData.getInstance(); | ||
} | ||
|
||
public static DataRepository getInstance() { | ||
return instance; | ||
} | ||
|
||
public List<UserModel> getLocalData() { | ||
return localData.getUserList(); | ||
} | ||
|
||
public UserModel getUserByName(String name) { | ||
return localData.getUser(name); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
mvvm/src/main/java/org/zhiwei/jetpack/mvvm/persistent/VirtualLocalData.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.zhiwei.jetpack.mvvm.persistent; | ||
|
||
|
||
import org.zhiwei.jetpack.mvvm.model.UserModel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Author: zhiwei. | ||
* Date: 2018/11/6 0006,20:07. | ||
*/ | ||
public class VirtualLocalData { | ||
private static VirtualLocalData instance = new VirtualLocalData(); | ||
|
||
private VirtualLocalData() { | ||
} | ||
|
||
public static VirtualLocalData getInstance() { | ||
return instance; | ||
} | ||
|
||
public UserModel getUser(String name) { | ||
UserModel usr = null; | ||
for (UserModel userModel : getUserList()) { | ||
if (userModel.getName().equals(name)) { | ||
usr = userModel; | ||
} | ||
} | ||
return usr; | ||
} | ||
|
||
public List<UserModel> getUserList() { | ||
List<UserModel> users = new ArrayList<>(); | ||
UserModel user; | ||
for (int i = 0; i < 20; i++) { | ||
user = new UserModel(); | ||
user.setAge(20 + i); | ||
user.setName("小明 " + i); | ||
user.setCity("上海:" + i); | ||
users.add(user); | ||
} | ||
return users; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
mvvm/src/main/java/org/zhiwei/jetpack/mvvm/view/MvvmActivity.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,62 @@ | ||
package org.zhiwei.jetpack.mvvm.view | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.TextView | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.Observer | ||
import org.zhiwei.jetpack.mvvm.R | ||
import org.zhiwei.jetpack.mvvm.model.UserModel | ||
import org.zhiwei.jetpack.mvvm.viewmodel.UserViewModel | ||
|
||
/** | ||
* 作者: 志威 zhiwei.org | ||
* 主页: Github: https://github.com/zhiwei1990 | ||
* 日期: 2019年08月05日 19:52 | ||
* 签名: 天行健,君子以自强不息;地势坤,君子以厚德载物。 | ||
* _ _ _ _ ____ _ _ _ | ||
* / \ _ __ __| |_ __ ___ (_) __| | / ___|| |_ _ _ __| (_) ___ | ||
* / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \ | ||
* / ___ \| | | | (_| | | | (_) | | (_| | ___) | |_| |_| | (_| | | (_) | | ||
* /_/ \_\_| |_|\__,_|_| \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/ -- 志威 zhiwei.org | ||
* | ||
* You never know what you can do until you try ! | ||
* ---------------------------------------------------------------- | ||
* Android Architecture Components 的架构演示界面 | ||
*/ | ||
class MvvmActivity : AppCompatActivity() { | ||
|
||
private val viewModel: UserViewModel by viewModels() | ||
|
||
private var tvList: TextView? = null | ||
private var tvInfo: TextView? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_aac) | ||
tvList = findViewById(R.id.tv_user_list_aac) | ||
tvInfo = findViewById(R.id.tv_user_info_aac) | ||
|
||
viewModel.userList.observe( | ||
this, | ||
Observer { userModels: List<UserModel> -> | ||
val sb = StringBuilder() | ||
for (model in userModels) { | ||
sb.append(model.toString()).append("\n") | ||
} | ||
tvList!!.text = sb.toString() | ||
} | ||
) | ||
viewModel.userInfo.observe( | ||
this, | ||
Observer { s: String -> tvInfo?.text = s } | ||
) | ||
} | ||
|
||
fun load(view: View?) { | ||
viewModel.loadData() | ||
viewModel.loadUserName("小明 3") | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
mvvm/src/main/java/org/zhiwei/jetpack/mvvm/viewmodel/UserViewModel.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.zhiwei.jetpack.mvvm.viewmodel; | ||
|
||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MediatorLiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
import org.zhiwei.jetpack.mvvm.model.UserModel; | ||
import org.zhiwei.jetpack.mvvm.persistent.DataRepository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Author: zhiwei. | ||
* Date: 2018/11/6 0006,19:20. | ||
*/ | ||
public class UserViewModel extends ViewModel { | ||
|
||
MutableLiveData<List<UserModel>> userList = new MutableLiveData<>(); | ||
MediatorLiveData<String> userInfo = new MediatorLiveData<>(); | ||
private DataRepository repository; | ||
|
||
public UserViewModel() { | ||
super(); | ||
repository = DataRepository.getInstance(); | ||
} | ||
|
||
public void loadData() { | ||
userList.setValue(repository.getLocalData()); | ||
} | ||
|
||
public void loadUserName(String name) { | ||
userInfo.setValue(repository.getUserByName(name).toString()); | ||
} | ||
|
||
public LiveData<List<UserModel>> getUserList() { | ||
return userList; | ||
} | ||
|
||
public LiveData<String> getUserInfo() { | ||
return userInfo; | ||
} | ||
} |
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,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/tv_user_list_aac" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="user info" | ||
android:textSize="12sp" /> | ||
|
||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:onClick="load" | ||
android:text="加载数据" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_user_info_aac" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="user info" /> | ||
</LinearLayout> |
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