• 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);
        }
    }
  • 相关阅读:
    HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
    环境变量设置错误导致全部命令无法使用解决办法
    正则表达式学习一
    opencv使用中的一点经验记录
    机器学习公开课回顾(一)
    bp算法的一个简单例子
    Ubuntu 16.04安装有道词典
    使用html2canvas库实现一个简单截图功能,还需优化
    PHP-MySQL连接封装-其他简单封装
    PHP获取客户端真实IP
  • 原文地址:https://www.cnblogs.com/BlogCommunicator/p/7390111.html
Copyright © 2020-2023  润新知