小结:getApplication()即是全局的上下文
- Activity中进行数据监听
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import cn.tsou.google2019io.R;
import cn.tsou.google2019io.livedata.viewmodel.LiveDataContextViewModel;
import cn.tsou.google2019io.livedata.viewmodel.LiveDataViewModel;
public class LiveDataContextActivity extends AppCompatActivity {
private LiveDataContextViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_data_context);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
viewModel = ViewModelProviders.of(this).get(LiveDataContextViewModel.class);
MutableLiveData liveData = viewModel.getLiveData();
liveData.observe(this, new Observer() {
@Override
public void onChanged(@Nullable String s) {
Log.d(“huangxiaoguo”, s);
}
});
}
public void onNormalSetUse(View view) {
viewModel.onNormalSetUse();
}
}
ViewModel中传递参数
viewmodel传递数据一般需要自定义Factory
- ViewModel
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.util.Log;
public class LiveDataSetDataViewModel extends ViewModel {
private String mKey;
private String mKey1;
public LiveDataSetDataViewModel(String key, String key1) {
this.mKey = key;
this.mKey1 = key1;
}
public static class Factory implements ViewModelProvider.Factory {
private String mKey;
private String mKey1;
public Factory(String key, String key1) {
mKey = key;
mKey1 = key1;
}
@Override
public T create(Class modelClass) {
return (T) new LiveDataSetDataViewModel(mKey, mKey1);
}
}
private MutableLiveData mLiveData;
public MutableLiveData getLiveData() {
if (mLiveData == null) {
mLiveData = new MutableLiveData<>();
}
return mLiveData;
}
public void onNormalSetUse() {
Log.d(“huangxiaoguo”, mKey + “------” + mKey1);
getLiveData().setValue(“hahahhahaha===>set”);
}
@Override
protected void onCleared() {
super.onCleared();
}
}
- Acticity
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import cn.tsou.google2019io.R;
import cn.tsou.google2019io.livedata.viewmodel.LiveDataSetDataViewModel;
public class LiveDataSetDataActivity extends AppCompatActivity {
private LiveDataSetDataViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_data_set_data);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
viewModel = ViewModelProviders.of(this, new LiveDataSetDataViewModel.Factory(“传递的参数”,“传递的参数1”)).get(LiveDataSetDataViewModel.class);
MutableLiveData liveData = viewModel.getLiveData();
liveData.observe(this, new Observer() {
@Override
public void onChanged(@Nullable String s) {
Log.d(“huangxiaoguo”, s);
}
});
}
public void onNormalSetUse(View view) {
viewModel.onNormalSetUse();
}
}
自定义LiveData
自定义LiveData一般都是重写onActive 和onInactive ,这里就拿一篇网上现成的例子来说明(范例来源:gdutxiaoxu的博客)
这里我们以观察网络状态变化为例子讲解
首先我们自定义一个 Class NetworkLiveData,继承 LiveData,重写它的 onActive 方法和 onInactive 方法
在 onActive 方法中,我们注册监听网络变化的广播,即ConnectivityManager.CONNECTIVITY_ACTION。在 onInactive 方法的时候,我们注销广播。
public class NetworkLiveData extends LiveData {
private final Context mContext;
static NetworkLiveData mNetworkLiveData;
private NetworkReceiver mNetworkReceiver;
private final IntentFilter mIntentFilter;
private static final String TAG = “NetworkLiveData”;
public NetworkLiveData(Context context) {
mContext = context.getApplicationContext();
mNetworkReceiver = new NetworkReceiver();
mIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
}
public static NetworkLiveData getInstance(Context context) {
if (mNetworkLiveData == null) {
mNetworkLiveData = new NetworkLiveData(context);
}
return mNetworkLiveData;
}
@Override
protected void onActive() {
super.onActive();
Log.d(TAG, “onActive:”);
mContext.registerReceiver(mNetworkReceiver, mIntentFilter);
}
@Override
protected void onInactive() {
super.onInactive();
Log.d(TAG, "onInactive: ");
mContext.unregisterReceiver(mNetworkReceiver);
}
private static class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
getInstance(context).setValue(activeNetwork);
}
}
}
这样,当我们想监听网络变化的时候,我们只需要调用相应的 observe 方法即可,方便又快捷。
NetworkLiveData.getInstance(this).observe(this, new Observer() {
@Override
public void onChanged(@Nullable NetworkInfo networkInfo) {
Log.d(TAG, “onChanged: networkInfo=” +networkInfo);
}
});
小结:如果想要在数据传递之前对数据进行统一操作,重写setValue方法即可,因为postValue最终调用的还是setValue。
Fragment中使用
在fragment中livaData的使用和Activity相同
- ViewModel
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
public class LiveDataFragmentViewModel extends ViewModel {
private MutableLiveData mLiveData;
public MutableLiveData getLiveData() {
if (mLiveData == null) {
mLiveData = new MutableLiveData<>();
}
return mLiveData;
}
public void onNormalSetUse() {
getLiveData().setValue(“LiveData3Fragment===>set”);
}
public void onNormalPostUse() {
new Thread(new Runnable() {
@Override
public void run() {
getLiveData().postValue(“LiveData3Fragment===>post”);
}
}).start();
}
}
- Fragment中使用
package cn.tsou.google2019io.livedata.mylivedata.fragment;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import cn.tsou.google2019io.R;
import cn.tsou.google2019io.livedata.viewmodel.LiveDataFragmentViewModel;
/**
- Created by Administrator on 2017/7/6 0006.
*/
public class LiveData3Fragment extends Fragment implements View.OnClickListener {
private LiveDataFragmentViewModel viewModel;
/**
- 简单使用setValue
*/
private Button mBtnNormalSet;
/**
- 简单使用postValue
*/
private Button mBtnNormalPost;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_livedata, container, false);
initView(mView);
return mView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(LiveDataFragmentViewModel.class);
MutableLiveData liveData = viewModel.getLiveData();
liveData.observe(this, new Observer() {
@Override
public void onChanged(@Nullable String s) {
Log.d(“huangxiaoguo”, s);
}
});
}
private void initView(View mView) {
mBtnNormalSet = (Button) mView.findViewById(R.id.btn_normal_set);
mBtnNormalSet.setOnClickListener(this);
mBtnNormalPost = (Button) mView.findViewById(R.id.btn_normal_post);
mBtnNormalPost.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn_normal_set:
viewModel.onNormalSetUse();
break;
case R.id.btn_normal_post:
viewModel.onNormalPostUse();
break;
}
}
}
Fragment之间传值
- ViewModel
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
public class LiveDataFragmentViewModel extends ViewModel {
private MutableLiveData mLiveData;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
写在最后
对程序员来说,很多技术的学习都是“防御性”的。也就是说,我们是在为未来学习。我们学习新技术的目的,或是为了在新项目中应用,或仅仅是为了将来的面试。但不管怎样,一定不能“止步不前”,不能荒废掉。
![
文章以下内容会给出阿里与美团的面试题(答案+解析)、面试题库、Java核心知识点梳理等,需要这些文档资料的,直接点击我的GitHub免费领取~文章来源地址https://www.toymoban.com/news/detail-853883.html
…(img-ZgLKef9R-1711288397007)]
[外链图片转存中…(img-GUk9EiCd-1711288397008)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-S6qoWoQ4-1711288397008)]
写在最后
对程序员来说,很多技术的学习都是“防御性”的。也就是说,我们是在为未来学习。我们学习新技术的目的,或是为了在新项目中应用,或仅仅是为了将来的面试。但不管怎样,一定不能“止步不前”,不能荒废掉。
[外链图片转存中…(img-45cY9HJl-1711288397009)]
[外链图片转存中…(img-m1IGIKt6-1711288397009)]
[外链图片转存中…(img-1rFVKyAG-1711288397010)]
[外链图片转存中…(img-S3wUVM7G-1711288397010)]文章来源:https://www.toymoban.com/news/detail-853883.html
文章以下内容会给出阿里与美团的面试题(答案+解析)、面试题库、Java核心知识点梳理等,需要这些文档资料的,直接点击我的GitHub免费领取~
到了这里,关于Android Architecture Components ——LiveData使用,android热修复视频的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!