android ViewModel&LiveData使用场景说明&案例分析(第二节)

前言

本篇文章主要讲解LiveData工作的原理,如果还不知道LiveData如何用的话,请参考官方文档
LiveData的讲解涉及到了Lifecycle的知识,如果你还不了解LifeCycle,请参考文档LifeCycle介绍

LiveData 是一个可以被观察的数据持有类,它可以感知 Activity、Fragment或Service 等组件的生命周期。简单来说,他主要有一下优点。

它可以做到在组件处于激活状态的时候才会回调相应的方法,从而刷新相应的 UI。
不用担心发生内存泄漏
当 config 导致 activity 重新创建的时候,不需要手动取处理数据的储存和恢复。它已经帮我们封装好了。
当 Actiivty 不是处于激活状态的时候,如果你想 livedata setValue 之后立即回调 obsever 的 onChange 方法,而不是等到 Activity 处于激活状态的时候才回调 obsever 的 onChange 方法,你可以使用 observeForever 方法,但是你必须在 onDestroy 的时候 removeObserver。
回想一下,在你的项目中,是不是经常会碰到这样的问题,当网络请求结果回来的时候,你经常需要判断 Activity 或者 Fragment 是否已经 Destroy, 如果不是 destroy,才更新 UI。而当你如果使用 Livedata 的话,因为它是在 Activity 处于 onStart 或者 onResume 的状态时,他才会进行相应的回调,因而可以很好得处理这个问题,不必谢一大堆的 activity.isDestroyed()。接下来,让我们一起来看一下 LiveData 的使用.


1569232134833.gif

LiveData是抽象类不能直接创建对象,MutableLiveData是LiveData一个非常基本的继承类。
例子中:是演示bind同一Activity 的多个Fragment间数据共享.我们在oneFragment中改变公共的viewmodel数据,在第twoFragment中绑定监听器.
好了,上代码:
viewModel代码:

public class MainView2Model extends ViewModel {
    private int itemA,itemB = 0;
    public MutableLiveData<String> itemALive = new MutableLiveData();
    public MutableLiveData<SparseArray<String>> itemListLive= new MutableLiveData();
    public void changeItem(){
        itemALive.postValue("刷新数据");
        SparseArray<String> list = new SparseArray<>();
        for (int i = 0 ;i < 10; i++ ){
            list.put(i,new Random().nextInt(100)+"");
        }
        itemListLive.postValue(list);
    }
}

Activity代码:

public class Main2Activity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
    MainView2Model mainViewModel;
    FrameLayout flContent;
    BottomNavigationBar bottomNavigationBar;
    private ArrayList<Fragment> fragments = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        flContent = findViewById(R.id.flContent);
        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        mainViewModel = ViewModelProviders.of(this).get(MainView2Model.class);

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED)
                .setInActiveColor(R.color.color_ff333333)
                .setActiveColor(R.color.colorPrimary)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_practice, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_course, "啦啦").setActiveColorResource(R.color.color_ffd7b176))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my, "我的")).setFirstSelectedPosition(0).initialise();
        fragments = new ArrayList<>();
        fragments.add(new oneFragment());
        fragments.add(new TwoFragment());
        fragments.add(new ThreeFragment());
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment bookFragment = fragments.get(0);
        transaction.replace(R.id.flContent, bookFragment);
        transaction.commit();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(int position) {
        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                if (fragment.isAdded()) {
                    ft.replace(R.id.flContent, fragment);
                } else {
                    ft.add(R.id.flContent, fragment);
                }
                ft.commitAllowingStateLoss();
            }
        }
    }

    @Override
    public void onTabUnselected(int position) {
        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    @Override
    public void onTabReselected(int position) {

    }
}

Fragment代码:

public class oneFragment extends Fragment {
    private View view;
    MainView2Model mainView2Model;
    private TextView oneTv;
    private TextView oneTv1;
    private Button oneBt;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_one, container, false);
        oneTv = view.findViewById(R.id.oneTv);
        oneTv1 = view.findViewById(R.id.oneTv1);
        oneBt = view.findViewById(R.id.oneBt);

        mainView2Model = ViewModelProviders.of(this.getActivity()).get(MainView2Model.class);
        mainView2Model.itemALive.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String string) {
               // oneTv.setText(string);
            }
        });

        oneBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //给被观察者MutableLiveData的对象设置值,值改变了,就会回调上面的观察监听器
                mainView2Model.changeItem();
               // Toast.makeText(oneFragment.this.getActivity(),"ssss",Toast.LENGTH_SHORT).show();
            }
        });

        mainView2Model.itemListLive.observe(this, new Observer<SparseArray<String>>() {
            @Override
            public void onChanged(@Nullable SparseArray<String> stringSparseArray) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0;i<stringSparseArray.size();i++){
                    sb.append(stringSparseArray.get(i)+";");
                }
                oneTv1.setText(sb.toString());
            }
        });
        return view;
    }
}

package com.example.acandroidlisten.objectanimator.viewmodelDemo.fragment;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.acandroidlisten.objectanimator.R;
import com.example.acandroidlisten.objectanimator.viewmodelDemo.MainView2Model;

import java.util.List;

public class TwoFragment extends Fragment {
    private View view;
    private MainView2Model mainView2Model;
    TextView twoTv;
    RecyclerView twoRecyclerView;
    MyRecyclerViewAdapter myRecyclerViewAdapter = null;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_two, container, false);
        mainView2Model = ViewModelProviders.of(this.getActivity()).get(MainView2Model.class);
        twoTv = view.findViewById(R.id.twoTv);
        twoRecyclerView = view.findViewById(R.id.twoRecyclerView);
        twoRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        myRecyclerViewAdapter =  new MyRecyclerViewAdapter(twoRecyclerView,mainView2Model.itemListLive.getValue());
        twoRecyclerView.setAdapter(myRecyclerViewAdapter);

        mainView2Model.itemALive.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String string) { //创建一个观察监听器
                twoTv.setText(string);
            }
        });
        mainView2Model.itemListLive.observe(this, new Observer<SparseArray<String>>() {
            @Override
            public void onChanged(@Nullable SparseArray<String> stringSparseArray) { //创建一个观察监听器
                myRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }

    class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.RecyclerHolder> {
        private Context mContext;
        private SparseArray<String> dataList = new SparseArray<>();

        public MyRecyclerViewAdapter(RecyclerView recyclerView,SparseArray<String> dataList) {
            this.mContext = recyclerView.getContext();
            if (null != dataList) {
                this.dataList = dataList;
            }
        }

        public void setData(SparseArray<String> dataList) {
            if (null != dataList) {
                this.dataList = dataList;
                notifyDataSetChanged();
            }
        }

        @Override
        public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_fragment_two, parent, false);
            return new RecyclerHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerHolder holder, int position) {
            holder.textView.setText(dataList.get(position));
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        class RecyclerHolder extends RecyclerView.ViewHolder {
            TextView textView;

            private RecyclerHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.text_adapter1);
            }
        }
    }
}

Xml相关文件:

//activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:elevation="2dp"
        android:translationZ="2dp" />

</LinearLayout>


//fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/oneTv"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="第一页"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/oneTv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/oneTv"
        android:background="#ffeedd"
        android:gravity="center"
        android:padding="16dp"
        android:text="数据"
        android:textColor="@color/colorPrimary" />

    <Button
        android:id="@+id/oneBt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:textSize="16dp"
        android:padding="6dp"
        android:layout_marginBottom="16dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff"
        android:text="共享数据" />

</RelativeLayout>


<?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:orientation="vertical">
    <TextView
        android:id="@+id/twoTv"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="第二页"
        android:textColor="#ffffff"
        android:gravity="center"
        android:background="@color/colorPrimary"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/twoRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>

源码地址:https://github.com/sgl890226/objectanimator

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容