zoukankan      html  css  js  c++  java
  • Android中MVP的应用

    概述

    对于代码架构,适合的才是最好的,至于是选用MVC,还是MVP,还是MVVM
    一方面根据自己的擅长和喜好来决定,再一方面就是根据代码业务逻辑来决定
    如果代码中压根没有用到Model层,那引入MVP等框架,真的只会让代码结构更复杂,而体现不出框架的优势。
    不要为了使用框架而使用框架。

    MVP Contract

    public interface MediaListContract {
        /**
         * Presenter 接口则定义了该界面(功能)中所有的用户操作事件
         * presenter层实现了该接口,只关注业务层的逻辑相关
         * UI的更新只需调用View的状态方法
         */
        interface Presenter extends IPresenter {
            void getMedias();
        }
    
        /**
         * View接口定义了该界面(功能)中所有的UI状态情况
         * view层(Activity或Fragment)实现了该接口,只关注UI相关的状态更新
         * 所有事件操作都调用 presenter 来完成
         */
        interface View extends IView {
            void refreshList(List<MediaInfoBean> lstMedias);
            void showError(String msg);
        }
    }
    

    一般Activity

    public class MediaViewActivity extends BaseActivity {
        private ActivityMediaViewBinding mView;
        private ArrayList<String> lstFilePaths;
        private int currentIndex;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mView = ActivityMediaViewBinding.inflate(getLayoutInflater());
            setContentView(mView.getRoot());
            initView();
        }
    
        @SuppressLint("ClickableViewAccessibility")
        private void initView() {
            Intent intent = this.getIntent();
            Bundle bundle = intent.getExtras();
            //...
    
            //点击播放和暂停
            mView.videoShow.setOnClickListener(view -> {
                if (mView.videoShow.isPlaying()) mView.videoShow.pause();
                else mView.videoShow.start();
            });
            //设置循环播放
            mView.videoShow.setOnPreparedListener(mediaPlayer -> {
                //...
            });
            //设定控件的滑动事件
            mView.videoShow.setOnTouchListener(new View.OnTouchListener() {
                //...
            });
    
            mView.tvBg.setOnTouchListener(new View.OnTouchListener() {
                //...
            });
        }
    
        private void play() {
            //使用videoview控件进行视频播放
            mView.videoShow.setVideoPath(lstFilePaths.get(currentIndex));
            mView.videoShow.start();
        }
    }
    
  • 相关阅读:
    [转] 美股评论:远离波动的噪音
    [转] GDB 下 watch的使用
    [转] Web性能压力测试工具之ApacheBench(ab)详解
    [转] Ubuntu 12.04下LAMP安装配置 (Linux+Apache+Mysql+PHP)
    [转] 在 Linux 中怎样使用cp命令合并目录树
    [转] postgresql常用命令
    [转] 跟着美联储投资
    [转] 智能指针(三):unique_ptr使用简介
    关于前端开发
    [转] 美股评论:美国散户血泪辛酸
  • 原文地址:https://www.cnblogs.com/ymtianyu/p/15470371.html
Copyright © 2011-2022 走看看