jetpackJetpackandroid技术

Jetpack mvvm 三部曲(一) ViewModel

2020-09-30  本文已影响0人  三月四晴

先说明此系列都是用Java的不是Kotlin( ps.虽然我也想用kotlin但实力不允许)

Jetpack mvvm 三部曲(二) LiveData

官方的Jetpack的链接

ViewModel

提前放下本jetpak系列自己学习期间写的demo jetpackDemo

了解ViewModel

官方给的生命周期对比图 官方也强烈注意

使用ViewModel

allprojects {
    repositories {
        google()
        jcenter()
    }
}
dependencies {
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
}
public class MyViewModel extends ViewModel {
    private User user;
    private int count;
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)
        MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
        textView = findViewById(R.id.text);
        if(viewModel.getUser()!=null){
            refreshText();
        }
}
//这里是一个按钮的点击时间
  public void addUser(View view){
        User user  = new User();
        user.setName("张三");
        user.setAge(18);
        viewModel.setUser(user);
        refreshText();
    }
public void refreshText(){
        textView.setText(viewModel.getUser().getName()+"今年"+viewModel.getUser().getAge()+"岁");
  }
androidx.fragment.app.Fragment
 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = (TextView) inflater.inflate(R.layout.fragment_view_model,container,false);
        //这里表示和activity共享一个
        viewModel = new ViewModelProvider((ViewModelStoreOwner) getActivity()).get(MyViewModel.class);
        //单独私有的
        viewModel1 = new ViewModelProvider(this).get(MyViewModel.class);
        if(viewModel.getUser()!=null){
            rootView.setText(viewModel.getUser().getName()+"今年"+viewModel.getUser().getAge()+"岁");
        }
        return rootView;
    }
public class MyApplication extends Application implements ViewModelStoreOwner {
    private static ViewModelStore viewModelStore;
    private MainViewModel mainViewModel;
    public static ViewModelStoreOwner viewModelStoreOwner;

    @Override
    public void onCreate() {
        super.onCreate();
        viewModelStore = new ViewModelStore();
        mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        viewModelStoreOwner = this;
    }

    public static ViewModelStoreOwner getViewModelStoreOwner() {
        return viewModelStoreOwner;
    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return viewModelStore;
    }
}
 MainViewModel  mainViewModel = new ViewModelProvider((MyApplication) getApplication()).get(MainViewModel.class);
public class MyViewModel extends ViewModel {
    private User user;
    private int count;

    public MyViewModel() {
        this.user = new User("张三",11);
        this.count = 13;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
 public MyViewModel(User user, int count) {
        this.user = user;
        this.count = count;
    }
图1
图2
图3.1 图3.2
图4
图5
public class MainViewModelFactory implements ViewModelProvider.Factory {
    private String name;
    private int age;

    public MainViewModelFactory(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        return (T) new MyViewModel(new User(name,age),11);
    }
}
 MyViewModel viewModel = new ViewModelProvider(this,new MainViewModelFactory("张三",11)).get(MyViewModel.class);

本人也是刚介入Jetpack 写的不对不好的地方请大家指点

感兴趣的可以继续看这篇稍微深入扯下Jetpack的ViewModel

上一篇下一篇

猜你喜欢

热点阅读