facebook twitter hatena line email

「Android/カスタムListener」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(別のクラスにListenerを追加する)
行286: 行286:
 
         public abstract void onClose(CustomData data);
 
         public abstract void onClose(CustomData data);
 
     }
 
     }
 +
 +
==ListenerをinterfaceでなくabstractClassで作る==
 +
<pre>
 +
public abstract class CustomListener {
 +
public abstract void onLoad(Custom data);
 +
public abstract void onError(Exception e){};
 +
}
 +
</pre>
 +
option指定したい場合onErrorの引数有りとなしどちらでも良い
 +
<pre>
 +
public abstract class CustomListener {
 +
public abstract void onLoad(Custom data);
 +
public void onError(Exception e){};
 +
@Deprecated
 +
public void onError() {}
 +
}
 +
</pre>

2018年12月17日 (月) 15:49時点における版

独自Listenerを作る方法

足し算処理を例にする

-CustomData.java

public class CustomData {
   public int code;
   public String message;
   public CustomData(int code, String message) {
       this.code = code;
       this.message = message;
   }
}

-CustomListener.java

public interface CustomListener {
   void onLoad(CustomData data);
   void onStart(CustomData data);
   void onSuccess(CustomData data);
   void onError(CustomData data);
   void onFinish(CustomData data);
   void onClose(CustomData data);
}

-CustomTask.java

public class CustomTask {
   private CustomListener mListener;
   private CustomData data;
   CustomTask() {
       data = new CustomData(123, "hello");
   }
   public void exec(int num, int addnum, int sum) {
       mListener.onLoad(data);
       mListener.onStart(data);
       if (num + addnum == sum) {
           data.message = "success " + num + "+" + addnum + "=" + sum;
           mListener.onSuccess(data);
       } else {
           data.message = "error " + num + "+" + addnum + "!=" + sum;
           mListener.onError(data);
       }
       mListener.onFinish(data);
       mListener.onClose(data);
   }
   public void setCustomListener(CustomListener listener) {
       mListener = listener;
   }
}

-メイン処理(MainActivity.java)

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomListener listener = new CustomListener() {
            @Override
            public void onLoad(CustomData data) {
                Log.i("test", "task onLoad()");
            }
            @Override
            public void onStart(CustomData data) {
                Log.i("test", "task onStart()");
            }
            @Override
            public void onSuccess(CustomData data) {
                Log.i("test", "task onSuccess() code=" + data.code + " " + data.message);
            }
            @Override
            public void onError(CustomData data) {
                Log.i("test", "task onError() code=" + data.code + " " + data.message);
            }
            @Override
            public void onFinish(CustomData data) {
                Log.i("test", "task onFinish()");
            }
            @Override
            public void onClose(CustomData data) {
                Log.i("test", "task onClose()");
            }
        };
        CustomTask task = new CustomTask();
        task.setCustomListener(listener);
        task.exec(1, 1, 2); // 処理1
        task.exec(1, 1, 3); // 処理2
    }
}

処理1

test: onLoad()
test: onStart()
test: onSuccess() code=123 success 1+1=2
test: onFinish()
test: onClose()

処理2

test: onLoad()
test: onStart()
test: onError() code=123 error 1+1!=3
test: onFinish()
test: onClose()

Listenerをメインスレッドで処理しない場合

-MainActivityの以下を書き換える

 -      CustomTask task = new CustomTask();
 +      CustomTask task = new CustomTask(this);

-CustomTask.java

public class CustomTask {
   private Activity mActivity;
   private CustomListener mListener;
   private CustomData data;
   CustomTask(Activity activity) {
       mActivity = activity;
       data = new CustomData(123, "hello");
   }
   public void exec(int num, int addnum, int sum) {
       this.mActivity.runOnUiThread(new Runnable() {
           public void run() {
               if (mListener != null) {
                   mListener.onLoad(data);
                   mListener.onStart(data);
               }
           }
       });
       if (num + addnum == sum) {
           data.message = "success " + num + "+" + addnum + "=" + sum;
           this.mActivity.runOnUiThread(new Runnable() {
               public void run() {
                   if (mListener != null) {
                       mListener.onSuccess(data);
                   }
               }
           });
       } else {
           data.message = "error " + num + "+" + addnum + "!=" + sum;
           this.mActivity.runOnUiThread(new Runnable() {
               public void run() {
                   if (mListener != null) {
                       mListener.onError(data);
                   }
               }
           });
       }
       this.mActivity.runOnUiThread(new Runnable() {
           public void run() {
               if (mListener != null) {
                   mListener.onFinish(data);
                   mListener.onClose(data);
               }
           }
       });
   }
   public void setCustomListener(CustomListener listener) {
       mListener = listener;
   }
}

privateクラスに定義したlistenerを使う

privateクラスにlistenerを定義して使うこともできる

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       CustomListener listener = new Custom2Listener(); // ここの部分
       CustomTask task = new CustomTask();
       task.setCustomListener(listener);
       task.exec(1, 1, 2);
       task.exec(1, 1, 3);
   }
   private class Custom2Listener implements CustomListener {
       @Override
       public void onLoad(CustomData data) {
           Log.i("test", "task onLoad()");
       }
       @Override
       public void onStart(CustomData data) {
           Log.i("test", "task onStart()");
       }
       @Override
       public void onSuccess(CustomData data) {
           Log.i("test", "task onSuccess() code=" + data.code + " " + data.message);
       }
       @Override
       public void onError(CustomData data) {
           Log.i("test", "task onError() code=" + data.code + " " + data.message);
       }
       @Override
       public void onFinish(CustomData data) {
           Log.i("test", "task onFinish()");
       }
       @Override
       public void onClose(CustomData data) {
           Log.i("test", "task onClose()");
       }
   }
}

privateプロパティに定義したlistenerを使う

privateプロパティにlistenerを定義して使うこともできる

public class MainActivity extends AppCompatActivity {
   private CustomListener mListener = new CustomListener() {
       @Override
       public void onLoad(CustomData data) {
           Log.i("test", "task onLoad()");
       }
       @Override
       public void onStart(CustomData data) {
           Log.i("test", "task onStart()");
       }
       @Override
       public void onSuccess(CustomData data) {
           Log.i("test", "task onSuccess() code=" + data.code + " " + data.message);
       }
       @Override
       public void onError(CustomData data) {
           Log.i("test", "task onError() code=" + data.code + " " + data.message);
       }
       @Override
       public void onFinish(CustomData data) {
           Log.i("test", "task onFinish()");
       }
       @Override
       public void onClose(CustomData data) {
           Log.i("test", "task onClose()");
       }
   };
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       CustomTask task = new CustomTask();
       task.setCustomListener(mListener);
       task.exec(1, 1, 2);
       task.exec(1, 1, 3);
   }
}

別のクラスにListenerを追加する

別のクラスにListenerを定義して使うこともできる

public class MainActivity extends AppCompatActivity {
   private CustomView.SampleListener mListener = new CustomView.SampleListener() {
       @Override
       public void onLoad(CustomData data) {
           Log.i("test", "task onLoad()");
       }
       @Override
       public void onStart(CustomData data) {
           Log.i("test", "task onStart()");
       }
       @Override
       public void onSuccess(CustomData data) {
           Log.i("test", "task onSuccess() code=" + data.code + " " + data.message);
       }
       @Override
       public void onError(CustomData data) {
           Log.i("test", "task onError() code=" + data.code + " " + data.message);
       }
       @Override
       public void onFinish(CustomData data) {
           Log.i("test", "task onFinish()");
       }
       @Override
       public void onClose(CustomData data) {
           Log.i("test", "task onClose()");
       }
   };
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       CustomTask task = new CustomTask();
       task.setCustomListener(mListener);
       task.exec(1, 1, 2);
       task.exec(1, 1, 3);
   }
}

SampleView.java

public class SampleView extends View {
   public static abstract interface SampleListener
   {
       public abstract void onLoad(CustomData data);
       public abstract void onStart(CustomData data);
       public abstract void onSuccess(CustomData data);
       public abstract void onError(CustomData data);
       public abstract void onFinish(CustomData data);
       public abstract void onClose(CustomData data);
   }

ListenerをinterfaceでなくabstractClassで作る

public abstract class CustomListener {
	public abstract void onLoad(Custom data);
	public abstract void onError(Exception e){};
}

option指定したい場合onErrorの引数有りとなしどちらでも良い

public abstract class CustomListener {
	public abstract void onLoad(Custom data);
	public void onError(Exception e){};
	@Deprecated
	public void onError() {}
}