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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

然后在几个.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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

MainContract.java.ftl

package ${packageName};

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

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

    }

    interface Presenter extends BasePresenter {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

还需要在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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

到这里好像就配置完了,可以试一下了,先重启下android studio。 
下边图示创建步骤吧,懒得写字 
这里写图片描述
Item是template中default的默认值,自己可以改,如Task,然后finish就o了。 
这里写图片描述

这里写图片描述这里写图片描述

打开来看看模板代码是否正确 
这里写图片描述
这里写图片描述 
这里写图片描述

猜你喜欢

转载自blog.csdn.net/sqq_yj/article/details/80450676
MVP