2015年12月9日 星期三

Android MVP 設計模式 Spinner練習

練習MVP設計模式,以Spinner為例
將檔案切成Model View Presenter


1.在Layout放置一個Spinner元件
2.View - MainActivity.java
package com.terryyamg.mvptest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

//View
public class MainActivity extends Activity implements MainActivityView {

    private Spinner spinner;
    private ArrayAdapter<String> listAdapter;

    private MainActivityPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);

        presenter = new MainActivityPresenterImpl(this);
        presenter.guide(); //導向
    }

    //Override MainActivityView 設定Adapter
    @Override
    public void setAdapter(String[] data) {
        listAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, data);
        listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    //Override MainActivityView 設定Spinner動作
    @Override
    public void setSpinner(final String[] data) {
        spinner.setAdapter(listAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int pos1, long arg3) {
                Toast.makeText(MainActivity.this, data[pos1], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    }
}

3.View - MainActivityView.java
package com.terryyamg.mvptest;

public interface MainActivityView {

    void setAdapter(String[] data);

    void setSpinner(String[] data);

}

4.Presenter - MainActivityPresenter.java
package com.terryyamg.mvptest;

public interface MainActivityPresenter {

    void guide();

}

5.Presenter - MainActivityPresenterImpl.java
package com.terryyamg.mvptest;

//Presenter
public class MainActivityPresenterImpl implements MainActivityPresenter {

    private MainActivityView mView;
    private MainActivityModel mModel;

    public MainActivityPresenterImpl(MainActivityView mView) {
        this.mView = mView;
        this.mModel = new MainActivityModelImpl();
    }


    @Override
    public void guide() {
        mModel.setData(mView); //導向Model
    }
}

6.Model - MainActivityModel.java
package com.terryyamg.mvptest;

public interface MainActivityModel {

    void setData(MainActivityView view);

}

7.Model - MainActivityModelImpl.java
package com.terryyamg.mvptest;

//Model
public class MainActivityModelImpl implements MainActivityModel {

    @Override
    public void setData(MainActivityView view) {
        String[] data = {"A", "B", "C", "D", "E"};

        //放入資訊
        view.setAdapter(data);
        view.setSpinner(data);
    }
}

完成後就可以得到一個與一般的spinner相同的效果

檔案下載:
 https://github.com/terryyamg/MVPTest
參考連結:
 https://github.com/antoniolg/androidmvp

1 則留言 :

  1. Their own acquired on your weblog even though deciding acknowledgement just a few tid little bit submits.
    joker123

    回覆刪除