谷歌官方MVP架构学习

时间:2022-07-19 21:12:31
Contract封装了View和
Presenter 



public interface TasksContract {

interface View extends BaseView<Presenter> {

void setLoadingIndicator(boolean active);

void showTasks(List<Task> tasks);

void showAddTask();

void showTaskDetailsUi(String taskId);

void showTaskMarkedComplete();

void showTaskMarkedActive();

void showCompletedTasksCleared();

void showLoadingTasksError();

void showNoTasks();

void showActiveFilterLabel();

void showCompletedFilterLabel();

void showAllFilterLabel();

void showNoActiveTasks();

void showNoCompletedTasks();

void showSuccessfullySavedMessage();

boolean isActive();

void showFilteringPopUpMenu();
}

interface Presenter extends BasePresenter {

void result(int requestCode, int resultCode);

void loadTasks(boolean forceUpdate);

void addNewTask();

void openTaskDetails(@NonNull Task requestedTask);

void completeTask(@NonNull Task completedTask);

void activateTask(@NonNull Task activeTask);

void clearCompletedTasks();

void setFiltering(TasksFilterType requestType);

TasksFilterType getFiltering();
}
}

//BaseView.java
public interface BaseView<T> {

void setPresenter(T presenter);

}

//BasePresenter.java
public interface BasePresenter {

void start();

}


Google将Fragment看作一个V而不是Activity,所以这里用Fragment来实现V接口。

public class ShowActivity extends BaseActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fragment);
}

@Override
protected void findViews() {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user_profile, menu);
return true;
}

@Override
protected void init() {
UserProfileShowFragment fragment =
(UserProfileShowFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
if (fragment == null) {
fragment = UserProfileShowFragment.newInstance();
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
fragment, R.id.contentFrame);
}
UserProfileShowPresenter presenter = new UserProfileShowPresenter(fragment, this);
fragment.setPresenter(presenter);
}
}


public class UserProfileShowFragment extends BaseFragment implements UserProfileShowContract.View {



private UserProfileShowContract.Presenter mPresenter;

public static UserProfileShowFragment newInstance() {
Bundle args = new Bundle();
UserProfileShowFragment fragment = new UserProfileShowFragment();
fragment.setArguments(args);
return fragment;
}

public void setPresenter(@NonNull UserProfileShowContract.Presenter presenter) {
mPresenter = presenter;
}
}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <FrameLayout        android:id="@+id/contentFrame"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/id_toolbar"></FrameLayout></RelativeLayout>