AndroidStudio創建MVP模板

最近仿照官方MVP模式開發,如下:
這裏寫圖片描述
每個模塊都需要寫Activity,Contract,Fragment,Presenter。覺得好麻煩,就想能不能一次創建這幾個類,去網上一搜,看到了鴻洋大神的博客Android Studio自定義模板 寫頁面竟然可以如此輕鬆
寫到在android studio的D:\android-studio\plugins\android\lib\templates\activities 這個目錄下存在着androidstudio新建項目的模板,拷過來改改就可以了
這裏寫圖片描述

複製一個Activity的文件夾重命名爲我的MVPActivity,內部有如下幾個文件
這裏寫圖片描述
root文件夾下是模板代碼
globals.xml.ftl
recipe.xml.ftl
template.xml
最後一個圖片是新建項目時的展示圖

首先我把我的activity,fragment,contract,persenter幾個類拷貝到root\src\app_package\下邊,並重命名爲xxx.java.ftl文件

然後再globals中修改成

<?xml version="1.0"?>
<globals>
    <#assign Collection=extractLetters(objectKind)>//從輸入的title中獲取輸入字符
    <#assign collection_name=Collection?lower_case>//獲取到的字符轉成小寫
    <#include "../common/common_globals.xml.ftl" />
    <global id="activity_layout" value="${Collection?lower_case}" />//作爲activity的layout的命名
    <global id="fragment_layout" value="${Collection?lower_case}" />//作爲activity的layout的命名
    <global id="ActivityName" value="${Collection}Activity" />//作爲activity類名
    <global id="FragmentName" value="${Collection}Fragment" />//作爲fragment類名
    <global id="PresenterName" value="${Collection}Presenter" />//作爲presenter類名
    <global id="ContractName" value="${Collection}Contract" />//作爲contract類名

    <global id="excludeMenu" type="boolean" value="true" />
    <global id="generateActivityTitle" type="boolean" value="false" />
</globals>

然後在幾個.java.ftl類中做配置,用globals中的配置名代替
MainActivity.java.ftl

package ${packageName};

import android.os.Bundle;

import ${applicationPackage}.R;
import ${applicationPackage}.base.BaseActivity;
import ${applicationPackage}.util.ActivityUtils;

public class ${ActivityName} extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_${activity_layout});


        ${FragmentName} ${fragment_layout}Fragment =
                (${FragmentName}) getSupportFragmentManager().findFragmentById(R.id.id_fragment_container);

        if (${fragment_layout}Fragment == null) {
            ${fragment_layout}Fragment = ${FragmentName}.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), ${fragment_layout}Fragment, R.id.id_fragment_container);
        }
        new ${PresenterName}(${fragment_layout}Fragment);
    }
}

MainContract.java.ftl

package ${packageName};

import ${applicationPackage}.BasePresenter;
import ${applicationPackage}.BaseView;

public interface ${ContractName} {
    interface View extends BaseView<Presenter> {

    }

    interface Presenter extends BasePresenter {

    }
}

MainFragment.java.ftl

package ${packageName};

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import ${applicationPackage}.R;
import ${applicationPackage}.base.BaseFragment;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * View
 */
public class ${FragmentName} extends BaseFragment implements ${ContractName}.View {
    private ${ContractName}.Presenter mPresenter;

    public static ${FragmentName} newInstance() {
        return new ${FragmentName}();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    @Override
    public void setPresenter(${ContractName}.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_${fragment_layout}, container, false);
        return root;
    }
}

MainPresenter.java.ftl

package ${packageName};

import android.support.annotation.NonNull;
import rx.subscriptions.CompositeSubscription;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Presenter
 */
public class ${PresenterName} implements ${ContractName}.Presenter {
    private ${ContractName}.View mLoginView;
    private CompositeSubscription mSubscription;

    public ${PresenterName}(@NonNull ${ContractName}.View ${activity_layout}View) {
        mLoginView = checkNotNull(${activity_layout}View);
        mSubscription = new CompositeSubscription();
        mLoginView.setPresenter(this);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscription.clear();
    }
}

還需要在recipe.xml.ftl中做如下配置。就是新建activity的時候需要創建哪些到哪裏。

<?xml version="1.0"?>
<recipe>
    <instantiate from="root/res/layout/activity_main.xml.ftl"
                   to="${escapeXmlAttribute(resOut)}/layout/activity_${activity_layout}.xml" />
    <instantiate from="root/res/layout/fragment_main.xml.ftl"
                   to="${escapeXmlAttribute(resOut)}/layout/fragment_${fragment_layout}.xml" />

    <instantiate from="root/src/app_package/MainActivity.java.ftl"
                   to="${escapeXmlAttribute(srcOut)}/${ActivityName}.java" />
    <instantiate from="root/src/app_package/MainFragment.java.ftl"
                   to="${escapeXmlAttribute(srcOut)}/${FragmentName}.java" />
    <instantiate from="root/src/app_package/MainPresenter.java.ftl"
                   to="${escapeXmlAttribute(srcOut)}/${PresenterName}.java" />
    <instantiate from="root/src/app_package/MainContract.java.ftl"
                   to="${escapeXmlAttribute(srcOut)}/${ContractName}.java" />


</recipe>

template.xml複製過來沒做什麼修改
裏面的name是創建時顯示的名字,改成自己的MVPActivity

<?xml version="1.0"?>
<template
    format="5"
    revision="5"
    name="MVPActivity"
    minApi="7"
    minBuildApi="14"
    description="Creates a new empty activity">

    <category value="Activity" />
    <formfactor value="Mobile" />

    <parameter
        id="objectKind"
        name="Object Kind"
        type="string"
        constraints="nonempty"
        default="Item"
        help="Other examples are 'Person', 'Book', etc." />
    <parameter
        id="objectKindPlural"
        name="Object Kind Plural"
        type="string"
        constraints="nonempty"
        default="Items"
        help="Other examples are 'People', 'Books', etc." />
    <parameter
        id="activityTitle"
        name="Title"
        type="string"
        constraints="nonempty"
        suggest="${objectKindPlural}"
        default="Items" />

    <parameter
        id="isLauncher"
        name="Launcher Activity"
        type="boolean"
        default="false"
        help="If true, this activity will have a CATEGORY_LAUNCHER intent filter, making it visible in the launcher" />

    <parameter
        id="packageName"
        name="Package name"
        type="string"
        constraints="package"
        default="com.mycompany.myapp" />

    <!-- 128x128 thumbnails relative to template.xml -->
    <thumbs>
        <!-- default thumbnail is required -->
        <thumb>template_blank_activity.png</thumb>
    </thumbs>

    <globals file="globals.xml.ftl" />
    <execute file="recipe.xml.ftl" />

</template>

到這裏好像就配置完了,可以試一下了,先重啓下android studio。
下邊圖示創建步驟吧,懶得寫字
這裏寫圖片描述
Item是template中default的默認值,自己可以改,如Task,然後finish就o了。
這裏寫圖片描述

這裏寫圖片描述這裏寫圖片描述

打開來看看模板代碼是否正確
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

應該沒啥問題了。

Github

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