MVP的模板

首先是Presenter:

public class DetailPresenter {
    DetailViewLayer mViewLayer;
    DetailModule mModule;
    String mAppPackageName;

    public DetailPresenter(String appPackageName, DetailViewLayer viewLayer) {
        this.mViewLayer = viewLayer;
        this.mAppPackageName = appPackageName;
    }

    void onActivityCreate() {
        /**
         * 初始化module
         */
        mModule = new DetailModule();
        loadData();
    }

    void onActivityDestroyed() {
        // TODO 目前什么都不做
    }

    void loadData() {
        // load data from module and let view show them
        mModule.query(mAppPackageName, new OnDataLoadedListener() {

            @Override
            public void onDataLoaded(List<AppDetailBean> appDetailBeanList) {
                AdapterView.OnItemClickListener onitemClickListener = new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                    }
                };

                mViewLayer.show(appDetailBeanList, onitemClickListener);
            }

            @Override
            public void onDataLoadFailed(Throwable e) {

            }
        });

    }

    public interface OnDataLoadedListener {
        void onDataLoaded(List<AppDetailBean> cardViewBeanList);

        void onDataLoadFailed(Throwable e);
    }
}

 

 

然后是VIewLayer:

public class MyViewLayer implements IMyView {
    GridView mGridView;
    BaseAdapter mAdapter;
    Context context;

    public MyViewLayer(Context context) {
        this.context = context;
        mGridView = new GridView(context);
        mGridView.setNumColumns(5);
        mGridView.setVerticalSpacing(60);
        mGridView.setPadding(100, 150, 100, 50);
    }

    @Override
    public void show(List<Bean> beanList,
            AdapterView.OnItemClickListener onclickListener) {
        mAdapter = new MyBaseAdapter(context, beanList);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(onclickListener);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateAdapterData(List<Bean> beanList,
            AdapterView.OnItemClickListener onclickListener) {
        show(beanList, onclickListener);
    }

    public View getInnerView() {
        return mGridView;
    }

}

 

 

 

在Activity中这样使用:

public class Content2Activity extends BaseActivity {
    Presenter mPresenter;
    IMyView mMyViewLayer;
    PageEnum mWhichPage;
    LinearLayout mll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content2);
        mll = (LinearLayout) findViewById(R.id.ll_content2);

        /**
         * 从传进来的intent得到whichpage
         */
        String whichPage = getIntent().getStringExtra("pageEnum");
        if (whichPage != null) {
            mWhichPage = PageEnum.valueOf(whichPage);
        }
        if (mWhichPage == null) {
            mWhichPage = PageEnum.gushixiaochengbao;
        }
        setBackground(mWhichPage);
        
        

        mMyViewLayer = new MyViewLayer(this);
        mPresenter = new Presenter(mWhichPage, mMyViewLayer);

        mPresenter.onActivityCreate();
        mll.addView(mMyViewLayer.getInnerView());
        // 要刷新吗? A:不用
    }

    /**
     * 根据pageEnum来设置背景图片
     * 
     * @param pageEnum
     */
    private void setBackground(PageEnum pageEnum) {
        int backgroundResId = -1;
        switch (pageEnum) {
        case anquanxiaozhishi:
            backgroundResId = R.drawable.safetytipsbg;
            break;
        case anzhuoxitong:
            //不应执行到这里
            break;
        case baibaoxiang:
            //不应执行到这里
            break;
        case dianduxuexi:
            //不应执行到这里
            break;
        case dongmanxuexileyuan:
            //不应执行到这里
            break;
        case gushi:
            backgroundResId = R.drawable.back_gushi;
            break;
        case gushixiaochengbao:
            backgroundResId = R.drawable.storycastlebg;
            break;
        case kexue:
            backgroundResId = R.drawable.back_kexue;
            break;
        case meishu:
            backgroundResId = R.drawable.back_meishu;
            break;
        case quweixiaomuwu:
            backgroundResId = R.drawable.funcabinsbg;
            break;
        case shehui:
            backgroundResId = R.drawable.back_shehui;
            break;
        case shuxue:
            backgroundResId = R.drawable.back_shuxue;
            break;
        case woshangyoueryuan:
            backgroundResId = R.drawable.nurserybg;
            break;
        case xiaoxiaoguoxuejia:
            backgroundResId = R.drawable.sinologybg;
            break;
        case yingyu:
            backgroundResId = R.drawable.back_yingyu;
            break;
        case yinyuexiaotiancai:
            backgroundResId = R.drawable.musicgeniusbg;
            break;
        case yuyan:
            backgroundResId = R.drawable.back_yuyan;
            break;
        case zhishileyuan:
            //不应执行到这里
            break;
        default:
            break;
        }
        mll.setBackgroundResource(backgroundResId);
    }
}

 

posted @ 2017-08-18 15:53  BlogCommunicator  阅读(249)  评论(0编辑  收藏  举报