AndroidStudio创建MVP模板

Posted 我的小侯子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="$CollectionActivity" />//作为activity类名
    <global id="FragmentName" value="$CollectionFragment" />//作为fragment类名
    <global id="PresenterName" value="$CollectionPresenter" />//作为presenter类名
    <global id="ContractName" value="$CollectionContract" />//作为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_layoutFragment =
                ($FragmentName) getSupportFragmentManager().findFragmentById(R.id.id_fragment_container);

        if ($fragment_layoutFragment == null) 
            $fragment_layoutFragment = $FragmentName.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), $fragment_layoutFragment, R.id.id_fragment_container);
        
        new $PresenterName($fragment_layoutFragment);
    

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_layoutView) 
        mLoginView = checkNotNull($activity_layoutView);
        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

以上是关于AndroidStudio创建MVP模板的主要内容,如果未能解决你的问题,请参考以下文章

Android MVP框架搭建与使用(含源码)

MVP+Kotlin源码体验

Android Studio插件之MVPHelper,一键生成MVP代码

RxAndroid + OkHttp + MVP(Android Studio)

从最简单的HelloWorld理解MVP模式

Android Studio 配置快速生成模板代码