基于Google MVVM框架的baseMVVM框架

基于Google MVVM框架的baseMVVM框架

前提說明:

本人在項目中的一些總結(jié)和參考大佬的代碼,綜合了一個小型的Android vvm框架
本框架適用于中小項目短纵,并且對DataBinging有一定的了解萝喘。mvvm模式對數(shù)據(jù)展示累項目的開發(fā)十分友好胡野。項目大或者頁面邏輯復雜的項目請繞行MVP
項目地址:gitHub地址
https://github.com/dingdaidao/baseMVVM

主要特性:

1.加入retrofit2 RxJava
2.使用GoogleArch大禮包,包含 LiveData ,ViewModel,LifeCycle
3.提供 DataBinding 自定義綁定事件(TextView,ImageView傲绣,)
4.集成張旭童的一行代碼實現(xiàn)RecyclerView 簡書鏈接
5.基于RxJava的 RxBus,RxUtils

如何使用:

1.activity巩踏,xml秃诵,和viewModel該怎么寫

繼承Commonlib下的BaseActivity,并傳入DataBinding生成的xml對應的ViewDataBing類和該頁面的ViewModel類

    public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {

注意ActivityMainBinding該類是根據(jù)activity_main.xml的命名來生成的。我們先來看下activity_main.xml文件

<!--跟布局用layout包裹-->
<layout 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">
    <!--聲明xml對應的ViewModel類-->
    <data>

        <variable
            name="mainVM"
            type="com.example.ding.vm.MainViewModel" />
    </data>
    <!--rootview-->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{mainVM.weatherLiveData}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
</layout>
@Override
   protected void dealWithAction(Event event) {
      //處理rxbus訂閱的信息
   }
@Override
   public int initContentView() {
      return R.layout.activity_main;
   }
@Override
   public int initVariableId() {
      return BR.mainVM;
   }

initContentView方法是為了獲取該頁面的xml塞琼,intiVariableId是獲取xml對應綁定的ViewModel(MainViewMode)的id

@Override
   public MainViewModel initViewModel() {
      return new MainViewModel();
   }

initViewModel是為了獲取對應viewModel的實例

@Override
    public void initViewObservable() {

    }

initViewObservable是為了初始化viewModel層產(chǎn)生數(shù)據(jù)變化時的監(jiān)聽事件菠净,比如viewModel中一個天氣情況的數(shù)據(jù)weatherLiveData產(chǎn)生了變化,則weatherLiveData.observe(this,(params){
//處理數(shù)據(jù)變化
});一般來說我們會把數(shù)據(jù)通過dataBinding直接塞給xml彪杉,View層毅往,但也不排除其他復雜一點的操作,在xml中無法處理派近。所以攀唯,可以在initViewObservable中初始化LiveData的監(jiān)聽事件。我們再來看下ViewModel類

public class MainViewModel extends BaseViewModel {
    private MutableLiveData<String> weatherLiveData = new MutableLiveData<>();

    @Override
    public void onCreate() {
        super.onCreate();
        //頁面初始化的一些操作
    }

    public MutableLiveData<String> getWeatherLiveData() {
        return weatherLiveData;
    }
    /**
     * 可不寫此方法
     * 使用getShowTxt.setValue(String weatherLiveData)
     *
     * @param weatherLiveData
     */
    public void setWeatherLiveData(String weatherLiveData) {
        this.weatherLiveData.setValue(weatherLiveData);
    }
}

在ViewModel類中定義的weatherLiveData屬性是繼承自LiveData渴丸,這是LiveData的介紹Android Developer LiveData革答。LiveData可以個DataBinding連用,實現(xiàn)雙向綁定曙强,數(shù)據(jù)驅(qū)動模型残拐。在view層中綁定LiveData,在LiveData中的數(shù)據(jù)產(chǎn)生變化時碟嘴,LiveData通過Observe通知到DataBinding綁定的控件溪食,更新顯示數(shù)據(jù)∧壬龋或者错沃,view產(chǎn)生變化的時候,view綁定的LiveData也會更著頁面更新雀瓢。

2.RecycleView的實現(xiàn)

recyclerView的實現(xiàn)參考了張旭童大神的方法枢析,上鏈接簡書鏈接先看下item中xml的實現(xiàn):

    <data>
        <variable
            name="data"
            type="String" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data}" />
    </LinearLayout>

定義傳入數(shù)據(jù)的類型String和對應name data(此為固定,因為已經(jīng)在item.xml中定義了通用id:data刃麸,這樣在代碼中配置adapter的時候就不用傳該item的variableId了)醒叁。

        BaseBindingAdapter mAdapter=new BaseBindingAdapter(this,R.layout.main_item);
        mBinding.r.setAdapter(mAdapter);
        mBinding.r.setAdapter(new BaseBindingAdapter(this,mListData,R.layout.main_item));

一兩行代碼即可配置好RecyclerView的適配器,是不是爽歪歪啊把沼。如果僅僅是普通的展示和點擊滿足不了你記得需求啊易,你還可以重寫onBindViewHolder方法。

        mAdapter=new BaseBindingAdapter(this,R.layout.main_item){
            //可復寫
            @Override
            public void onBindViewHolder(BaseBindingVH holder, int position) {
                super.onBindViewHolder(holder, position);//復寫是不可刪除
            }
        };

在這里你可以根據(jù)position對數(shù)據(jù)或者view做出相應的處理饮睬。對于多布局的RecyclerView可以使用BaseMulTypeBindingAdapter類去實現(xiàn)租谈,我就不過多解釋了。

最后捆愁,這是我的第一篇博客割去,求輕噴。后續(xù)我會繼續(xù)完善這個項目昼丑,并繼續(xù)更新文章分析Google Architecture Compontents組件庫和DataBinding的控件綁定用法劫拗。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市矾克,隨后出現(xiàn)的幾起案子页慷,更是在濱河造成了極大的恐慌,老刑警劉巖胁附,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件酒繁,死亡現(xiàn)場離奇詭異,居然都是意外死亡控妻,警方通過查閱死者的電腦和手機州袒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來弓候,“玉大人郎哭,你說我怎么就攤上這事」酱妫” “怎么了夸研?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長依鸥。 經(jīng)常有香客問我亥至,道長,這世上最難降的妖魔是什么贱迟? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任姐扮,我火速辦了婚禮,結(jié)果婚禮上衣吠,老公的妹妹穿的比我還像新娘茶敏。我一直安慰自己,他們只是感情好缚俏,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布惊搏。 她就那樣靜靜地躺著贮乳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪胀屿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天包雀,我揣著相機與錄音宿崭,去河邊找鬼。 笑死才写,一個胖子當著我的面吹牛葡兑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赞草,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼讹堤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了厨疙?” 一聲冷哼從身側(cè)響起洲守,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沾凄,沒想到半個月后梗醇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡撒蟀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年叙谨,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片保屯。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡手负,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出姑尺,到底是詐尸還是另有隱情竟终,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布切蟋,位于F島的核電站衡楞,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏敦姻。R本人自食惡果不足惜瘾境,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镰惦。 院中可真熱鬧迷守,春花似錦、人聲如沸旺入。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至礼华,卻和暖如春咐鹤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背圣絮。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工祈惶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人扮匠。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓捧请,卻偏偏與公主長得像,于是被迫代替她去往敵國和親棒搜。 傳聞我的和親對象是個殘疾皇子疹蛉,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容