Android mvvm框架應用,不需要關心多線程。

使用各種架構的目的無非是讓代碼變的簡潔,易讀。並且在多人開發中可以展現出無限的魅力。不同的層可以讓不同的人開發,互相獨立並互相影響!
框架就是將原本需要一大堆代碼的統一起來,來簡化代碼的編輯。

mvvm

Model-View-ViewModel的簡寫,mvvm就是一種設計模式,不懂的自己百度一下。在實際開發中也可以省去model變爲vvm。

FinalMvvm

我們在Android開發中用的最多的無非就是多線程,爲了避免手機卡頓,我們在做耗時操作時必須在子線程中執行,而執行完成後又需要在主線程中去做UI操作。 這樣就會需要寫很多的代碼,並且還要考慮子線程主線程的關係,邏輯複雜了之後就容易產生一些意想不到的bug讓我們頭疼。
這裏需要感謝rxjava,讓多線程變得不用我們管,哈哈!沒錯,finalMvvm使用了rxjava進行多線程處理,並將rxjava和mvvm完美結合。 我們寫代碼的時候無需關心rxjava,只需要根據 規則編輯代碼,便可以編輯出簡潔易讀並且性能優異的優秀代碼,哈哈- -
那就讓我們來看看這個神祕的finalMvvm吧!

上代碼

在使用前需要rxJava依賴

    implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

1.MainView

public interface MainView {
    /**
     * 顯示天氣的文字
     */
    void showWeatherText(String text);

}

2.MainActivity需要實現MainView接口,並且需要在class上加上@View註解以表示是View層

@View
public class MainActivity extends AppCompatActivity implements MainView{
    @Autowired
    MainViewModel mainViewModel;
    
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView);
        FinalMvvm.init(this);//初始化finalMvvm,框架自動掃描註解
    }
    @Override
    public void showWeatherText(String text) {
        textView.setText(text);
    }
}

3.ViewModel

public interface MainViewModel {
    void loadWeather();
}

4.MainViewModelImpl實現ViewModel,並且需要在class上方加上註解@ViewModel,以表示是ViewModel層

@ViewModel
public class MainViewModelImpl implements MainViewModel{
    @Autowired
    MainView mainView;

    @Autowired
    MainModel mainModel;

    @Override
    public void loadWeather() {
        /**
         * 此處做數據處理,處理完後,在主動讓view去修改UI
         */
        Weather weather=mainModel.loadWeatherFromUrl();//獲取天氣預報信息
        String reslut="";
        if(weather!=null){
            reslut="城市:"+weather.getCity()+"  最低溫度:"+weather.getTemp1()+"   最高溫度:"+weather.getTemp2()+"    天氣情況:"+weather.getWeather();
        }
        mainView.showWeatherText(reslut);
    }
}

5.MainModel直接新建一個model類加上@Model註解

@Model
public class MainModel {
    /**
     * 從服務器獲取天氣數據,對數據進行基本處理,轉成需要的格式(如json,bean,string等)
     * 不需要多線程,直接同步執行就行了
     */
    public Weather loadWeatherFromUrl(){
        String jsonStr=doHttp("http://www.weather.com.cn/data/cityinfo/101010100.html");
        JSONObject jo=JSON.parseObject(jsonStr);
        JSONObject weatherinfo=jo.getJSONObject("weatherinfo");
        Weather weather=weatherinfo.toJavaObject(Weather.class);
        return weather;
    }
    /**
     * 執行http請求
     * 這裏只是爲演示使用,建議自己使用okhttp等框架
     * 需要同步執行,不需要使用框架內的異步操作
     */
    public static String doHttp(String urlStr) {
        try {
            URL u = new URL(urlStr);
            InputStream in = u.openStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                byte buf[] = new byte[1024];
                int read;
                while ((read = in.read(buf)) > 0) {
                    out.write(buf, 0, read);
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
            byte b[] = out.toByteArray();
            String result=new String(b, "utf-8");
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "aaaaa";
    }
}

提醒

代碼看到這裏有人就有疑問了,爲什麼請求http沒有在子線程中執行,哈哈!告訴你們在viewmodel運行的時候就已經在子線程了。

完結

到這裏就完成了,這樣就層次很清晰了。並且不需要操作任何多線程的代碼,但實際在viewmodel的時候就已經在子線程執行了!

 

代碼連接:https://github.com/yuanfen7650/FinalMvvm

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