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();
        }
    }
    
  • 相关阅读:
    627. Swap Salary
    176. Second Highest Salary
    596. Classes More Than 5 Students
    183. Customers Who Never Order
    181. Employees Earning More Than Their Managers
    182. Duplicate Emails
    175. Combine Two Tables
    620. Not Boring Movies
    595. Big Countries
    HDU 6034 Balala Power! (贪心+坑题)
  • 原文地址:https://www.cnblogs.com/ymtianyu/p/15470371.html
Copyright © 2011-2022 走看看