Android mvp框架應用,不需要關心多線程。有完整的demo

github地址:https://github.com/yuanfen7650/FinalMvp


使用各種架構的目的無非是讓代碼變的簡潔,易讀。並且在多人開發中可以展現出無限的魅力。不同的層可以讓不同的人開發,互相獨立並互相影響!<br/>
框架就是將原本需要一大堆代碼的統一起來,來簡化代碼的編輯。
# mvp
Model-View-Presenter的簡寫,mvp就是一種設計模式,不懂的自己百度一下。
# Finalmvp
我們在Android開發中用的最多的無非就是多線程,爲了避免手機卡頓,我們在做耗時操作時必須在子線程中執行,而執行完成後又需要在主線程中去做UI操作。
這樣就會需要寫很多的代碼,並且還要考慮子線程主線程的關係,邏輯複雜了之後就容易產生一些意想不到的bug讓我們頭疼。<br/>
這裏需要感謝rxjava,讓多線程變得不用我們管,哈哈!沒錯,finalmvp使用了rxjava進行多線程處理,並將rxjava和mvp完美結合。
我們寫代碼的時候無需關心rxjava,只需要根據
規則編輯代碼,便可以編輯出簡潔易讀並且性能優異的優秀代碼,哈哈- -<br/>
那就讓我們來看看這個神祕的finalmvp吧!

# 上代碼
這裏做一個查詢天氣的demo<br/>
在使用前需要rxJava依賴
<pre>
    implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
</pre>

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

}
</pre>

2.MainActivity需要實現MainView接口,並且需要在class上加上@View註解以表示是View層
<pre>
@View
public class MainActivity extends AppCompatActivity implements MainView{
    @Autowired
    MainPresenter mainPresenter;
    
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView);
        Finalmvp.init(this);//初始化finalmvp,框架自動掃描註解
    }
    @Override
    public void showWeatherText(String text) {
        textView.setText(text);
    }
}
</pre>

3.Presenter
<pre>
public interface MainPresenter {
    void loadWeather();
}
</pre>
4.MainPresenterImpl實現Presenter,並且需要在class上方加上註解@Presenter,以表示是Presenter層<br/>
<pre>
@Presenter
public class MainPresenterImpl implements MainPresenter{
    @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);
    }
}
</pre>


5.MainModel直接新建一個model類加上@Model註解
<pre>
@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";
    }
}
</pre>
# 提醒
代碼看到這裏有人就有疑問了,爲什麼請求http沒有在子線程中執行,哈哈!告訴你們在Presenter運行的時候就已經在子線程了。

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

# 使用方法:

1.Add it in your root build.gradle at the end of repositories:
<pre>
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
</pre>
2.Add the dependency
<pre>
dependencies {
        implementation 'com.github.yuanfen7650:Finalmvp:1.9.5'
}
</pre>

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