liveDataBus使用篇

在工程的build.gradle配置

allprojects {
    repositories {
        google()
        jcenter()

        maven { url 'https://jitpack.io' }
    }
}

在model的build.gradle配置

dependencies {

implementation 'com.github.zhudaihao:livedatbus-master:1.0.1'

 

}

 

現在就可以愉快的開始玩耍了

我創建三個activity:MainActivity負責發送消息(全部訂閱者都可以接受 和創建後的訂閱的接);TextAllActivity和TextCreationActivity在消息發送後 才創建訂閱的;

MainActivity對應的xml代碼

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendAll"
        android:text="發佈所有訂閱者"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendCreation"
        android:text="發佈創建的訂閱者"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

 

代碼很簡單就兩個按鈕,對應MainActivity代碼

public class MainActivity extends AppCompatActivity {

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


    }

    /**
     * 發佈給 所有訂閱者
     *
     * @param view
     */
    public void sendAll(View view) {
        HomeBean homeBean = new HomeBean("張三");
        LiveDataBus.getDefault().with("homebean", HomeBean.class).postValue(homeBean);

        startActivity(new Intent(this,TextAllActivity.class));
    }


    /**
     * 發佈給 創建的訂閱者
     *
     * @param view
     */
    public void sendCreation(View view) {
        HomeBean homeBean = new HomeBean("李四");
        LiveDataBus.getDefault().withCreation("homebean", HomeBean.class).postValue(homeBean);

        startActivity(new Intent(this,TextCreationActivity.class));
    }

}

 

//TextAllActivity的XML代碼就一個按鈕就不寫了,看看Java代碼

/**
 * 測試 在發送後 訂閱的用戶
 */
public class TextAllActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        button=findViewById(R.id.bt);



        //訂閱
        LiveDataBus.getDefault().with("homebean", HomeBean.class).observe(this, new Observer<HomeBean>() {
            @Override
            public void onChanged(HomeBean homeBean) {
                //訂閱結果回調
                button.setText(homeBean.getName());

            }
        });

    }


}

 
//TextCreationActivity的XML代碼就一個按鈕就不寫了,看看Java代碼
/**
 * 測試 在發送後 訂閱的用戶
 */
public class TextCreationActivity extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creation);
        button=findViewById(R.id.bt);

        //訂閱
        LiveDataBus.getDefault().withCreation("homebean", HomeBean.class).observe(this, new Observer<HomeBean>() {
            @Override
            public void onChanged(HomeBean homeBean) {
                //訂閱結果回調
                button.setText(homeBean.getName());
            }
        });

    }


}

 

效果如下

 

 

這個我通過調用不同方法實現指定對象接收消息;

源碼地址:https://github.com/zhudaihao/livedatbus-master

 

 

 

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