王學崗41jetpack的使用

新建項目注意勾選Androidx
在這裏插入圖片描述
一,監聽生命週期

package com.example.lsn41_jetpack_20190920;

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

/**
 * 我們用這個觀查者來盯好需要感知生命週期的對象
 */
public class MyLifeObserver implements LifecycleObserver {
    ////通過註解,只要Activity的onStart()方法跑起來,我們就會執行這個方法
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void onStartJett(LifecycleOwner activity){
               Log.i("zhang_xin","onCreate:"+activity.getClass().getName());
  }
}

這裏最重要的就是這個註解,註解中除了Activity的常用的六個生命週期方法外,還有
Lifecycle.Event.ANY,表示在任何方法中調用
在onActivity中的onCreate()中或者在Fragment中調用

    //完成綁定,
        getLifecycle().addObserver(new MyLifeObserver());

看下運行效果
在這裏插入圖片描述
二:LiveDate和ViewModel
liveData可以用來保存數據,可以實時的通知UI更新
首先導入一個東西
implementation ‘androidx.lifecycle:lifecycle-extensions:2.0.0’
在這裏插入圖片描述
ViewModel的作用:將UI層數據和model層數據進行綁定

package com.example.lsn41_jetpack_20190920.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.lsn41_jetpack_20190920.R;
import com.example.lsn41_jetpack_20190920.bean.Girl;

import java.util.ArrayList;
import java.util.List;

public class MyGirl extends ViewModel {
    //定義一個對象,相當於一個用來存放數據的倉庫。MutableLiveData繼承自LiveData
    //注意這裏必須是static,不然會報空指針異常,因爲我們這裏一個ViewModel對應多個Activity
    private static MutableLiveData<List<Girl>> liveData;
    //用於獲取數據
    public LiveData<List<Girl>> getDataBean(){
        if(liveData==null){
            liveData=new MutableLiveData<>();
            loadData();
        }
        return liveData;
    }
    //數據可以從網絡或者線程讀取,我們這裏先寫死
    private void loadData() {
        List<Girl> data;
        data = new ArrayList<>();
        data.add(new Girl(R.drawable.f1, "一星", "****"));
        data.add(new Girl(R.drawable.f2, "一星", "****"));
        data.add(new Girl(R.drawable.f3, "一星", "****"));
        data.add(new Girl(R.drawable.f4, "一星", "****"));
        data.add(new Girl(R.drawable.f5, "一星", "****"));
        data.add(new Girl(R.drawable.f6, "一星", "****"));
        data.add(new Girl(R.drawable.f7, "一星", "****"));
        data.add(new Girl(R.drawable.f8, "一星", "****"));
        data.add(new Girl(R.drawable.f9, "一星", "****"));
        data.add(new Girl(R.drawable.f10, "一星", "****"));

        //把這些數據存放到倉庫裏面
        liveData.setValue(data);
    }

    //提供一個方法來改變數據,修改數據不需要使用EventBus,非常方便
    public void changeValue(int item,int i){
    //我把集合的內容改了,然後在放回去
        List<Girl> value = liveData.getValue();
        value.get(item).setLike(i+"");
        liveData.setValue(value);

    }
}

Girl是一個bean類,就不貼出來了,我們在MainActivity的onCreate()中調用

//調用系統API初始化這個對象,不需要使用new關鍵字。
        myGirl= ViewModelProviders.of(this).get(MyGirl.class);
        //myGirl.getDataBean()是得到這個倉庫,設置一個人去監聽這個倉庫
        myGirl.getDataBean().observe(this, new Observer<List<Girl>>() {
            /**
             * 當我們的數據發生變化的時候,我們可以在這個onChanged中進行處理
             * @param girls
             */
            @Override
            public void onChanged(List<Girl> girls) {
                listView.setAdapter(new GirlAdapter(MainActivity.this,girls));
            }
        });

長按改變事件

  listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                myGirl.changeValue(position,1);
          return false;
            }
        });

在這裏插入圖片描述
大家可以發現這個更新是一個實時更新
即便是調到另一個Activity也一樣可以更新數據

 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                myGirl.changeValue(position,1);
                Intent intent=new Intent(MainActivity.this,SecActivity.class);
                MainActivity.this.startActivity(intent);
                return false;
            }
        });
package com.example.lsn41_jetpack_20190920;

import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProviders;

import com.example.lsn41_jetpack_20190920.bean.Girl;
import com.example.lsn41_jetpack_20190920.viewmodel.MyGirl;

import java.util.List;

public class SecActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyGirl myGirl= ViewModelProviders.of(this).get(MyGirl.class);
        //一定要在主線程中調用,如果是在子線程,可以使用Handler把他發送到主線程
        //在這裏修改數據,返回後發現數據已經改變,比eventbus好用,這句代碼你可以寫在任何地方
        myGirl.changeValue(0,666);
    }
}

當然以後我們會進一步完善,會把ViewModel和佈局綁定
應用場景:股票實時變化
三,Activity與fragment之間的數據通信,我們以前多用EventBus,我們現在用livedata實現
我們去淘寶買華爲手機,下訂單後,華爲手機發現後會給我們手機,在這裏淘寶網就是總線。這是一個標準的觀察者模式,華爲手機會觀察淘寶網。華爲手機就是發佈者,淘寶網是代理,我們普通用戶是訂閱者。
華爲手機的實體類,


    package com.example.lsn41_jetpack_livedatabus_20190920;

public class Huawei {
    public String type;

    public Huawei(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


//這個類是我們的總線,就相當於我們買手機例子中的淘寶網
public class LiveDataBus {
    //用Map存放訂閱者,用MutableLiveData存放數據
    private Map<String, MutableLiveData<Object>> bus;

    //單例
    private static LiveDataBus liveDataBus=new LiveDataBus();
    private LiveDataBus(){
        bus=new HashMap<>();
    }
    public static LiveDataBus getInstance(){
        return liveDataBus;
    }

    /**
     * 用來給用戶進行訂閱(存入map)
     * type給系統反射調用
     * 因爲用的人多,考慮到多線程問題
     */
    public synchronized<T> MutableLiveData<T> with(String key,Class<T> type){
    //根據key值判斷用戶是否訂閱過,如果已經訂閱過直接去拿,沒有就存到map裏
        if(!bus.containsKey(key)){
            bus.put(key,new MutableLiveData<Object>());
        }
        return (MutableLiveData<T>) bus.get(key);
    }
   }
package com.example.lsn41_jetpack_livedatabus_20190920;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //消費者訂閱消息,消費者把信息存到Map中,廠家根據Map發貨
        LiveDataBus.getInstance().with("huawei",Huawei.class)
                //訂閱一個觀察者,系統中的代碼
                .observe(this,new Observer<Huawei>() {
                    @Override
                    public void onChanged(Huawei huawei) {
                        if(huawei!=null){
                            //點擊sendMessage所在的按鈕,會執行這句代碼
                            Toast.makeText(MainActivity.this, huawei.getType(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

    /**
     * 這裏就是一個發佈者(蘋果,華爲),廠家發貨
     * @param view
     */
    public void sendMessage(View view) {
        //生產了一臺華爲手機
        Huawei huawei=new Huawei("META-20");
        //廠家發佈消息,“huawei”這一字符串與消費者訂閱消息的地方保持一致
        //手機發送到(淘寶的)Map中,然後在發送到訂閱者(顧客)中
        LiveDataBus.getInstance().with("huawei",Huawei.class).postValue(huawei);
    }
    }

我們點擊發送按鈕,會根據Map中的要求將這臺手機存到LiveDataBs中,然後LiveDataBus會將消息返回給訂閱者。最終執行的效果就是彈出"META-20"
我們可以在任意的Activitty和任意的Fragment中發送消息,發到任意的一個位置,可以在Activity和Fragemnt通信,比EventBus方便多了,注意,它不能跨進程。
四:如第三點,也會遇到一些問題,我們現在新建一個Activity,內容與MainActivity一樣,MainActivity中添加一個頁面跳轉的方法,

 public void startSecActivity(View view) {
        Intent intent=new Intent(this,SecActivity.class);
        startActivity(intent);
    }

我們點擊MainActivity中的發送消息,然後在點擊跳轉按鈕進入新建的Activity頁面。這個時候新建頁面在沒有點擊本界面發送消息的情況下依然會彈出"META-20"
當我們執行postValue(huawei);方法的時候,mVersion就會加一,mLastVersion>=mVersion的時候,就不會執行onChanged()。通過反射拿到兩個變量的值,另他們相等,這就是我們今天的思路。

package com.example.lsn41_jetpack_livedatabus_20190920;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 這個類就是我們的總線(天貓,京東),要存放所有訂閱者的信息
 */
public class LiveDataBus {
    //存放訂閱者,用BusMutableLiveData存放數據
    private Map<String, BusMutableLiveData<Object>> bus;

    //單例
    private static LiveDataBus liveDataBus=new LiveDataBus();
    private LiveDataBus(){
        bus=new HashMap<>();
    }
    public static LiveDataBus getInstance(){
        return liveDataBus;
    }

    /**
     * 用來給用戶進行訂閱(存入map)
     * type給系統反射調用
     */
    public synchronized<T> BusMutableLiveData<T> with(String key,Class<T> type){
        if(!bus.containsKey(key)){
            bus.put(key,new BusMutableLiveData<Object>());
        }
        return (BusMutableLiveData<T>) bus.get(key);
    }

    public static class BusMutableLiveData<T> extends MutableLiveData<T>{
        @Override
        public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
            super.observe(owner, observer);
            hook(observer);
        }

        private void hook(Observer<? super T> observer) {
            try{
                //1.得到mLastVersion
                Class<LiveData> liveDataClass=LiveData.class;
                Field mObserversField = liveDataClass.getDeclaredField("mObservers");
                //只要不是public都要改
                mObserversField.setAccessible(true);
                //獲取到這個成員變量對應的對象
                Object mObserversObject = mObserversField.get(this);
                //得到map
                Class<?> mObserversObjectClass = mObserversObject.getClass();
                //獲取到mObservers對象的get方法
                Method get=mObserversObjectClass.getDeclaredMethod("get",Object.class);
                get.setAccessible(true);
                //執行get方法
                Object invokeEntry=get.invoke(mObserversObject,observer);
                //取到map中的value
                Object observerWraper=null;
                if(invokeEntry!=null && invokeEntry instanceof Map.Entry){
                    observerWraper=((Map.Entry)invokeEntry).getValue();
                }
                if(observerWraper==null){
                    throw new NullPointerException("observerWraper is null");
                }
                //得到ObserverWrapper的類對象
                Class<?> superclass=observerWraper.getClass().getSuperclass();
                Field mLastVersion = superclass.getDeclaredField("mLastVersion");
                mLastVersion.setAccessible(true);


                //2.得到mVersion
                Field mVersion = liveDataClass.getDeclaredField("mVersion");
                mVersion.setAccessible(true);

                //3.把mVersion的值填入到mLastVersion中
                Object mVersionValue=mVersion.get(this);
                mLastVersion.set(observerWraper,mVersionValue);

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

}



    package com.example.lsn41_jetpack_livedatabus_20190920;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //消費者訂閱消息
        LiveDataBus.getInstance().with("huawei",Huawei.class)
                //訂閱一個觀察者
                .observe(this,new Observer<Huawei>() {
                    @Override
                    public void onChanged(Huawei huawei) {
                        if(huawei!=null){
                            //點擊sendMessage所在的按鈕,會執行這句代碼
                            Toast.makeText(MainActivity.this, huawei.getType(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

    /**
     * 這裏就是一個發佈者(蘋果,華爲),廠家發貨
     * @param view
     */
    public void sendMessage(View view) {
        //生產了一臺華爲手機
        Huawei huawei=new Huawei("META-20");
        //廠家發佈消息,“huawei”這一字符串與消費者訂閱消息的地方保持一致
        //手機發送到(淘寶的)Map中,然後在發送到訂閱者(顧客)中
        LiveDataBus.getInstance().with("huawei",Huawei.class).postValue(huawei);
    }


    public void startSecActivity(View view) {
        Intent intent=new Intent(this,SecActivity.class);
        startActivity(intent);
    }
}


    package com.example.lsn41_jetpack_livedatabus_20190920;

public class Huawei {
    public String type;

    public Huawei(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


package com.example.lsn41_jetpack_livedatabus_20190920;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

public class SecActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //消費者訂閱消息
        LiveDataBus.getInstance().with("huawei",Huawei.class).observe(
                this,
                new Observer<Huawei>() {//觀查者
                    @Override
                    public void onChanged(Huawei huawei) {
                        if(huawei!=null){
                            Toast.makeText(SecActivity.this, huawei.getType(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }

        );
    }
    /**
     * 發佈者
     * @param view
     */
    public void sendMessage(View view){
        Huawei huawei=new Huawei("META-20");
        //廠家發佈消息
        LiveDataBus.getInstance().with("huawei",Huawei.class).postValue(huawei);
    }
}

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

    <Button
        android:onClick="sendMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發佈消息"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:onClick="startSecActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳轉"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

五:我們看下在Fragment中是如何通信的。

package com.example.lsn43_livedataandviewmodel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

public class FragmentTwo extends Fragment {

    private TextView textName;
    private NameViewModel model;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        textName = view.findViewById(R.id.tv_text);

        //獲取viewModel
        model = ViewModelProviders.of(getActivity()).get(NameViewModel.class);
        //監聽值的變化
        //model.getmCurrentName().observeForever();可以在任何時候得到數據
        model.getmCurrentName().observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                //更新UI
                textName.setText(s);
            }
        });
        return view;
    }
}

package com.example.lsn43_livedataandviewmodel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

public class FragmentOne extends Fragment {

    private EditText edContent;
    private Button btnSend;
    private NameViewModel model;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        edContent = view.findViewById(R.id.et_content);
        btnSend = view.findViewById(R.id.btn_send);

        //獲取viewModel
        model = ViewModelProviders.of(getActivity()).get(NameViewModel.class);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //獲取到liveData後設置liveData的值
                model.getmCurrentName().setValue(edContent.getText().toString());
            }
        });
        return view;
    }
}


package com.example.lsn43_livedataandviewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class NameViewModel extends ViewModel {

    //liveData通常和viewModel一起使用
    private MutableLiveData<String> mCurrentName;

    public MutableLiveData<String> getmCurrentName() {
        if (mCurrentName == null) {
            mCurrentName = new MutableLiveData<String>();
        }
        return mCurrentName;
    }

    //如果需要可以在這裏釋放資源
    @Override
    protected void onCleared() {
        super.onCleared();
    }
}

Activity中有兩個Fragment,上一個fragment是一個輸入框和一個按鈕,輸入文字後單擊按鈕,下面的fragment會顯示輸入的數據,並且在屏幕旋轉的時候,數據也不會丟失。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章