美文网首页
android架构之一(todo-mvp).md

android架构之一(todo-mvp).md

作者: 会飞的大象 | 来源:发表于2016-06-15 14:17 被阅读147次

    原文链接:https://github.com/googlesamples/android-architecture/tree/todo-mvp/

    概要

    此篇架构是介绍纯粹的mvp模式,不包含任何框架,数据通过手工注入,异步任务通过回调完成。
    链接包含完整的项目示例,由google工程师维护。


    职责:

    • contact:定义 view 和 presenter
    • activity:负责Views(fragments)和presenters的创建
    • fragment:实现view接口
    • presenter:实现presenter接口

    一般情况,
    业务逻辑在presenter,依赖view来做UI操作;
    view几乎不含逻辑,将presenter的命令变为UI动作,监听用户操作,传递给presenter处理
    contacts 用来定义views和presenters之间的联系

    代码分析:

    TasksActivity.java

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tasks_act);
    
    ...省略
    
            TasksFragment tasksFragment =
                    (TasksFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
            if (tasksFragment == null) {
                // Create the fragment
                tasksFragment = TasksFragment.newInstance();
                ActivityUtils.addFragmentToActivity(
                        getSupportFragmentManager(), tasksFragment, R.id.contentFrame);
            }
    
            // Create the presenter
            mTasksPresenter = new TasksPresenter(
                    Injection.provideTasksRepository(getApplicationContext()), tasksFragment);
    
    ...省略
        }
    

    在activity中创建fragment和presenter的实例
    TasksFragment.java

    public class TasksFragment extends Fragment implements TasksContract.View {
    
        private TasksContract.Presenter mPresenter;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mListAdapter = new TasksAdapter(new ArrayList<Task>(0), mItemListener);
        }
    
        @Override
        public void onResume() {
            super.onResume();
            mPresenter.start();
        }
    
        @Override
        public void setPresenter(@NonNull TasksContract.Presenter presenter) {
            mPresenter = checkNotNull(presenter);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.tasks_frag, container, false);
    
    ...省略
    
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    mPresenter.loadTasks(false);
                }
            });
    
    ...省略
            return root;
        }
    
    }
    

    fragment实现view接口,通过setPresenter()获取presenter的引用,通过presenter加载数据。
    TasksPresenter.java

    public class TasksPresenter implements TasksContract.Presenter {
    
        private final TasksRepository mTasksRepository;
    
        private final TasksContract.View mTasksView;
    
        public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) {
            mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
            mTasksView = checkNotNull(tasksView, "tasksView cannot be null!");
    
            mTasksView.setPresenter(this);
        }
    
    }
    

    实现presenter接口,通过构造方法获取view(fragment)的引用,同时将自身的引用传递给view(fragment)。
    这在activity中实例化presenter时已经完成

    类图如下:



    去掉接口,简化图


    相关文章

      网友评论

          本文标题:android架构之一(todo-mvp).md

          本文链接:https://www.haomeiwen.com/subject/fsjglttx.html