AndroidStudio で設定しておきたいコードテンプレート集

# Fragment

API Level 11 以降ならこちら。

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end

import android.app.Fragment;
import android.os.Bundle;

#parse("File Header.java")
public class ${NAME} extends Fragment {
    public static final String TAG = ${NAME}.class.getSimpleName();

    // Note: only the system can call this constructor by reflection. 
    public ${NAME}() {}

    public static ${NAME} getInstance() {
        ${NAME} fragment = new ${NAME}();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
}
```

サポートパッケージを使う場合は

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end

import android.support.v4.app.Fragment
import android.os.Bundle;

#parse("File Header.java")
public class ${NAME} extends Fragment {
    public static final String TAG = ${NAME}.class.getSimpleName();

    // Note: only the system can call this constructor by reflection. 
    public ${NAME}() {}

    public static ${NAME} getInstance() {
        ${NAME} fragment = new ${NAME}();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
}
```

# データ構造を表す型

Parcelable を実装して Entity を作るとき。

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;

#parse("File Header.java")
public class ${NAME} implements Parcelable{
    public static final String TAG = ${NAME}.class.getSimpleName();
    public static final Creator<${NAME}> CREATOR = new Creator<${NAME}>() {
        @Nullable
        @Override
        public ${NAME} createFromParcel(Parcel source) {
            return new ${NAME}(source);
        }

        @Override
        public ${NAME}[] newArray(int size) {
            return new ${NAME}[size];
        }
    }

    public ${NAME}(Parcel source) {
        
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        
    }
}
```

Builder を使いたい場合は以下。

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;

#parse("File Header.java")
public class ${NAME} implements Parcelable{
    public static final String TAG = ${NAME}.class.getSimpleName();
    public static final Creator<${NAME}> CREATOR = new Creator<${NAME}>() {
        @Nullable
        @Override
        public ${NAME} createFromParcel(Parcel source) {
            return new ${NAME}(source);
        }

        @Override
        public ${NAME}[] newArray(int size) {
            return new ${NAME}[size];
        }
    }

    // Note: only the builder of this type can call this constructor.
    private ${NAME} () {}

    public ${NAME}(Parcel source) {
        
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        
    }

    public static class Builder {
        private final ${NAME} mTarget;

        public Builder() {
            mTarget = new ${NAME}();
        }

        public ${NAME} create() {
            return mTarget;
        }
    }
}
```

# Utility

継承を禁止し、コンストラクタを封印、リフレクションで呼ばれたら例外にするなど。

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME};#end
#parse("File Header.java")
public final class ${NAME} {
    private ${NAME}() {
        throw new AssertionError("constructor of the utility class should not be called");
    }
}
```

# Loader

ベーシックなローダ

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import android.support.v4.content.AsyncTaskLoader;

#parse("File Header.java")
public class ${NAME} extends AsyncTaskLoader<${RESULT}> {
    public static final String TAG = ${NAME}.class.getSimpleName();

    public ${NAME}(Context context) {
        super(context);
    }

    @Override
    public ${RESULT} loadInBackground() {
        return null;
    }

    @Override
    public void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }
}
```

ローカルキャッシュを効かせるなら

```java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import android.support.v4.content.AsyncTaskLoader;

#parse("File Header.java")
public class ${NAME} extends AsyncTaskLoader<${RESULT}> {
    public static final String TAG = ${NAME}.class.getSimpleName();
    private ${CACHE_TYPE} mCache;

    public ${NAME}(Context context) {
        super(context);
    }

    @Override
    public ${RESULT} loadInBackground() {
        return null;
    }

    @Override
    public void deliverResult(${RESULT} data) {
        if (isReset()) {
            if (mCache != null) {
                mCache = null;
            }
            return;
        }

        mCache = data;

        if (isStarted()) {
            super.deliverResult(data);
        }
    }

    @Override
    public void onStartLoading() {
        if (this.result != null) {
            deliverResult(mCache);
        }
        if (takeContentChanged() || mCache == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        super.onStopLoading();
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();
        onStopLoading();
    }
}
```