简单了解MVP

当下Android开发使用MVP,MVVM使用的非常多,很多网站博客论坛都通过图文详细讲解了各种优缺点。然而感觉图画的再好感觉没有卵用。还是看代码来的直接。
然而发现各家代码写的有些不同,有好几个模板,其实大体都是相同的。都知道Google出了自己家的目标。
这里就简单记录下google家的模板代码写的一个界面。

效果图

最平淡无奇的登录界面。很多文章都有。

代码

代码结构

废话不多说,直接上代码吧。个人感觉文章写的再牛,都不如看过代码自己体验的好。所以只贴代码,不多说。

MainActivity.java就是一个容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public class MainActivity extends AppCompatActivity {

@BindView(R.id.fl_content)
FrameLayout flContent;
private MainPresenter mainPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MainFragment mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.fl_content);
if (mainFragment == null) {
mainFragment = MainFragment.newInstance();
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), mainFragment, R.id.fl_content);
}
mainPresenter = new MainPresenter(this,mainFragment);
mainFragment.setPresenter(mainPresenter);

}
}

MainFragment.java处理view

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

public class MainFragment extends Fragment implements MainContract.View {
@BindView(R.id.tiet_user_name)
TextInputEditText tietUserName;
@BindView(R.id.tiet_password)
TextInputEditText tietPassword;
@BindView(R.id.btn_login)
Button btnLogin;
@BindView(R.id.pb_progress_bar)
ProgressBar pbProgressBar;
Unbinder unbinder;
private MainContract.Presenter mPresenter;

public static MainFragment newInstance() {
MainFragment fragment = new MainFragment();
return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = new MainPresenter(getActivity(), this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}

@OnClick(R.id.btn_login)
public void login() {
showProgressBar();
mPresenter.login();
}

@Override
public void onDestroyView() {

super.onDestroyView();
unbinder.unbind();
}

@Override
public void showProgressBar() {
pbProgressBar.setVisibility(View.VISIBLE);
}

@Override
public void hideProgressBar() {
pbProgressBar.setVisibility(View.GONE);

}

@Override
public String getUserName() {
return tietUserName.getText().toString().trim();
}

@Override
public String getPassword() {
return tietPassword.getText().toString().trim();
}

@Override
public void setPresenter(MainContract.Presenter presenter) {
mPresenter = presenter;
}
}

MainContract .java定义接口通过接口来处理view,presenter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public interface MainContract {
interface View extends BaseView<Presenter> {
void showProgressBar();

void hideProgressBar();

String getUserName();


String getPassword();

}

interface Presenter extends BasePresenter {
void login();
}
}

MainPresenter .java处理逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

public class MainPresenter implements MainContract.Presenter {
private Context context;
private MainContract.View mView;

public MainPresenter(Context context, MainContract.View view) {

this.context = context;
mView = view;
}

@Override
public void login() {

if ("user".equals(mView.getUserName()) && "123".equals(mView.getPassword())) {
ToastUtils.showToast(context, "Login Successfully!");
mView.hideProgressBar();
} else {
ToastUtils.showToast(context, "Sorry,you failed!");
mView.hideProgressBar();
}
}
}

Demo下载