{"id":1702,"date":"2016-09-01T18:00:38","date_gmt":"2016-09-01T10:00:38","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1702"},"modified":"2026-08-18T12:58:51","modified_gmt":"2026-08-18T04:58:51","slug":"mvp%e6%a8%a1%e5%bc%8f%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1702","title":{"rendered":"MVP\u6a21\u5f0f\u5165\u95e8"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u5e94\u7528\u5f00\u53d1\u603b\u4f1a\u78b0\u5230 UI\u3001\u6570\u636e\u3001\u4e1a\u52a1\u903b\u8f91\u3002\u903b\u8f91\u4e00\u590d\u6742\u5c31\u8981\u5206\u6846\u67b6\u3002\u6700\u65e9\u662f MVC\uff0c\u540e\u6765\u4e5f\u7528\u5728 Android \u4e0a\u3002MVP \u53ef\u4ee5\u770b\u4f5c MVC \u7684\u5347\u7ea7\uff1a\u628a Activity \u4ece\u300c\u53c8\u5f53 View \u53c8\u5f53 Controller\u300d\u91cc\u62c6\u51fa\u6765\u3002<\/p>\n<\/blockquote>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e00\u3001\u6982\u8ff0\uff1aMVC \u4e3a\u4ec0\u4e48\u4f1a\u80d6<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">MVC \u67b6\u6784<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">M : \u4e1a\u52a1\u5c42\u548c\u6a21\u578b\u5c42\uff0c\u76f8\u5f53\u4e0ejavabean\u548c\u6211\u4eec\u7684\u4e1a\u52a1\u8bf7\u6c42\u4ee3\u7801\n   model\u5c42\u4e3b\u8981\u8d1f\u8d23\uff1a\n   \u00b7 \u4ece\u7f51\u7edc\uff0c\u6570\u636e\u5e93\uff0c\u6587\u4ef6\uff0c\u4f20\u611f\u5668\uff0c\u7b2c\u4e09\u65b9\u7b49\u6570\u636e\u6e90\u8bfb\u5199\u6570\u636e\u3002\n   \u00b7 \u5bf9\u5916\u90e8\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u89e3\u6790\u8f6c\u6362\u4e3aAPP\u5185\u90e8\u6570\u636e\u4ea4\u7531\u4e0a\u5c42\u5904\u7406\u3002\n   \u00b7 \u5bf9\u6570\u636e\u7684\u4e34\u65f6\u5b58\u50a8,\u7ba1\u7406\uff0c\u534f\u8c03\u4e0a\u5c42\u6570\u636e\u8bf7\u6c42\u3002\n\n V : \u89c6\u56fe\u5c42\uff0c\u5bf9\u5e94Android\u7684layout.xml\u5e03\u5c40\u6587\u4ef6\n C : \u63a7\u5236\u5c42\uff0c\u5bf9\u5e94\u4e8eActivity\u4e2d\u5bf9\u4e8eUI \u7684\u5404\u79cd\u64cd\u4f5c<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7eb8\u9762\u5f88\u6e05\u6670\uff0c\u5b9e\u9645\u5f00\u53d1\u91cc\u8bf7\u6c42\u4ee3\u7801\u7ecf\u5e38\u585e\u8fdb Activity\uff1blayout.xml \u53ea\u80fd\u7ed9\u9ed8\u8ba4 UI\uff0c\u89c6\u56fe\u53d8\u5316\u4e5f\u88ab\u4e22\u8fdb Activity\uff1bActivity \u672c\u8eab\u8fd8\u627f\u62c5\u63a7\u5236\u5c42\u3002\u4e8e\u662f Activity \u96c6\u9f50 MVC\uff0c\u4ece\u51e0\u767e\u884c\u6da8\u5230\u51e0\u5343\u884c\uff0c\u7ef4\u62a4\u6210\u672c\u8d8a\u6765\u8d8a\u9ad8\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u65b0\u7684 MVP \u67b6\u6784<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">M : \u8fd8\u662f\u4e1a\u52a1\u5c42\u548c\u6a21\u578b\u5c42\nV : \u89c6\u56fe\u5c42\u7684\u8d23\u4efb\u7531Activity\u6765\u62c5\u5f53\nP : \u65b0\u6210\u5458Prensenter \u7528\u6765\u4ee3\u7406 C(control) \u63a7\u5236\u5c42,\u662fView\u548cModel\u7684\u4e2d\u95f4\u201c\u6865\u6881\u201d\uff0c\u8d1f\u8d23View\u548cModel\u95f4\u7684\u4ea4\u4e92<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">MVP \u4e0e MVC \u6700\u5927\u7684\u4e0d\u540c\uff0c\u662f Activity \u7684\u804c\u8d23\uff1a\u4ece C\uff08\u63a7\u5236\u5c42\uff09\u53d8\u6210 V\uff08\u89c6\u56fe\u5c42\uff09\uff0c\u53ea\u7ba1\u600e\u4e48\u663e\u793a\u3002\u63a7\u5236\u4ea4\u7ed9 Presenter\u3002\u8fd9\u6837\u89e3\u5f00 Activity \u5bf9\u63a7\u5236\u5c42\u548c\u89c6\u56fe\u5c42\u7684\u8fc7\u5ea6\u8026\u5408\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/mvp.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1714\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/mvp.png\" alt=\"mvp\" width=\"550\" height=\"182\" \/><\/a><\/p>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all;\" width=\"817\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u89d2\u8272<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">MVC \u91cc\u8c01\u625b<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">MVP \u91cc\u8c01\u625b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Model<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Bean + \u8bf7\u6c42\uff08\u5e38\u88ab\u585e\u8fdb Activity\uff09<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u4ecd\u8d1f\u8d23\u6570\u636e\u6e90\u3001\u89e3\u6790\u3001\u6682\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">View<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">layout.xml\uff0c\u53d8\u5316\u5374\u5199\u5728 Activity<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Activity \/ Fragment \u53ea\u8d1f\u8d23\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u63a7\u5236<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Activity \u65e2\u662f C \u53c8\u63ba V<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Presenter \u5f53 View \u4e0e Model \u7684\u6865<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e8c\u3001\u5b9e\u4f8b<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u7528 MVP \u5b9e\u73b0\u4e0b\u56fe\u6548\u679c\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/cheap.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1716\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/cheap.png\" alt=\"cheap\" width=\"343\" height=\"436\" \/><\/a><\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7b2c\u4e00\u6b65 View \u5c42<\/h3>\n<p style=\"margin: 1em 0px;\">\u5148\u5b9a\u4e49 UI \u80fd\u505a\u4ec0\u4e48\uff0cFragment \u53ea\u5b9e\u73b0\u63a5\u53e3\u3001\u628a\u70b9\u51fb\u4ea4\u7ed9 Presenter\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/**\n * \u5b9a\u4e49UI\u89c6\u56fe\u903b\u8f91\n *\/\npublic interface ICheapView {\n    void setListViewAdapter(ReadNewsAdapter adpater);\n    void showToastMessage(String message);\n    void setBannerLayoutData(List&lt;AdsBean&gt; list);\n}\n\npublic class CheapFragment extends BaseMvpFragment&lt;ICheapView, CheapPresenter&gt; implements ICheapView, View.OnClickListener {\n    private EditText mSearchKey;\n    private ListView mListView;\n    private FrameLayout mListHeadView;\n    private BannerLayout mBannerLayout;\n\n    public static CheapFragment newInstance() {\n        CheapFragment fragment = new CheapFragment();\n        return fragment;\n    }\n\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n                             Bundle savedInstanceState) {\n        mListHeadView = (FrameLayout) inflater.inflate(fragment_cheap_banner_layout, null);\n        return inflater.inflate(R.layout.fragment_cheap_layout, container, false);\n    }\n\n    @Override\n    public void onActivityCreated(Bundle savedInstanceState) {\n        super.onActivityCreated(savedInstanceState);\n        mSearchKey = (EditText) getView().findViewById(R.id.com_search_edit);\n        mBannerLayout = (BannerLayout) mListHeadView.findViewById(R.id.banner);\n        mListView = (ListView) getView().findViewById(R.id.cheap_listview);\n        mListView.addHeaderView(mListHeadView);\n        mSearchKey.setOnClickListener(this);\n\n        mPresenter.setListViewAdapter();\n        mPresenter.setListBannerViewData();\n    }\n\n    @Override\n    public void onClick(View v) {\n        switch (v.getId()) {\n            case R.id.com_search_edit:\n                getActivity().onSearchRequested();\n                break;\n        }\n    }\n\n    @Override\n    public CheapPresenter initPresenter() {\n        return new CheapPresenter(getActivity());\n    }\n\n    @Override\n    public void setListViewAdapter(ReadNewsAdapter adpater) {\n        mListView.setAdapter(adpater);\n    }\n\n    @Override\n    public void showToastMessage(String message) {\n        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();\n    }\n\n    @Override\n    public void setBannerLayoutData(List&lt;AdsBean&gt; urls) {\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        for (AdsBean ads : urls) {\n            list.add(ads.getImgsrc());\n        }\n        mBannerLayout.setViewUrls(list);\n        \/\/\u6dfb\u52a0\u76d1\u542c\u4e8b\u4ef6\n        mBannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {\n            @Override\n            public void onItemClick(int position) {\n                showToastMessage(String.valueOf(position));\n            }\n        });\n    }\n}<\/code><\/pre>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7b2c\u4e8c\u6b65 Presenter \u5c42<\/h3>\n<p style=\"margin: 1em 0px;\">\u4e1a\u52a1\u63a5\u53e3\u7531 Presenter \u5b9e\u73b0\uff1a\u5411 Model \u8981\u6570\u636e\uff0c\u6210\u529f\u540e\u518d\u8c03 View\u3002<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/**\n * \u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\n *\/\npublic interface ICheapPresenter {\n    void setListViewAdapter();\n    void setListBannerViewData();\n}\n\npublic class CheapPresenter extends BasePresenter&lt;ICheapView&gt; implements ICheapPresenter{\n    private ICheapModel mCheapModel;\n    private ReadNewsAdapter mReadNewsAdapter;\n\n    public CheapPresenter(Context context) {\n        mCheapModel = new CheapModel();\n        mReadNewsAdapter = new ReadNewsAdapter(context);\n    }\n\n    @Override\n    public void setListViewAdapter() {\n        mView.setListViewAdapter(mReadNewsAdapter);\n    }\n\n    @Override\n    public void setListBannerViewData() {\n        mCheapModel.setListAndAdsData(new CallbackListener&lt;NewsTextBean&gt;() {\n            @Override\n            public void onSuccess(NewsTextBean data) {\n                List&lt;NewsTextItemBean&gt; newsTextItemBeanList = data.getT1348647909107();\n                mReadNewsAdapter.addDatas(0,newsTextItemBeanList);\n                List&lt;AdsBean&gt; adsLists = newsTextItemBeanList.get(0).getAds();\n                mView.setBannerLayoutData(adsLists);\n            }\n\n            @Override\n            public void onFailure(String message) {\n                mView.showToastMessage(message);\n            }\n        });\n    }\n}<\/code><\/pre>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u7b2c\u4e09\u6b65 Model \u5c42<\/h3>\n<p style=\"margin: 1em 0px;\">\u7f51\u7edc\u8bf7\u6c42\u653e\u5728 Model\uff0c\u8fd9\u91cc\u7528 Retrofit \u62c9\u7f51\u6613\u65b0\u95fb\u63a5\u53e3\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public interface ICheapModel {\n    void setListAndAdsData(final CallbackListener&lt;NewsTextBean&gt; listener);\n}\n\npublic class CheapModel implements ICheapModel {\n    @Override\n    public void setListAndAdsData(final CallbackListener&lt;NewsTextBean&gt; listener) {\n        Retrofit retrofit = new Retrofit.Builder()\n                .addConverterFactory(GsonConverterFactory.create())\n                .baseUrl(\"http:\/\/c.m.163.com\/\")\n                .build();\n\n        RetrofitCall call = retrofit.create(RetrofitCall.class);\n        Call&lt;NewsTextBean&gt; mCall = call.getNewsText();\n        mCall.enqueue(new Callback&lt;NewsTextBean&gt;() {\n            @Override\n            public void onResponse(Call&lt;NewsTextBean&gt; call, Response&lt;NewsTextBean&gt; response) {\n                NewsTextBean newsTextBean =  response.body();\n                listener.onSuccess(newsTextBean);\n            }\n\n            @Override\n            public void onFailure(Call&lt;NewsTextBean&gt; call, Throwable t) {\n                listener.onFailure(t.getMessage());\n\n            }\n        });\n    }\n}<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e09\u3001\u57fa\u7c7b\uff1a\u7ed1\u5b9a\u4e0e\u89e3\u5f00 View<\/span><\/h2>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">BaseMvpFragment<\/code> \u8d1f\u8d23\u5b9e\u4f8b\u5316 Presenter\uff1b<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">BasePresenter<\/code> \u6301\u6709 View \u5f15\u7528\uff0c\u9500\u6bc1\u65f6\u6e05\u6389\uff0c\u907f\u514d\u6cc4\u6f0f\u3002<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public abstract class BasePresenter&lt;T&gt; {\n    \/*\u8fd9\u4e2a\u57fa\u7c7b\u7684Presenter \u4e3b\u8981\u7684\u4f5c\u7528\u5c31\u662f\u5c06\u5f53\u524dPresenter\u6301\u6709\u7684view \u5728\u5408\u9002\u7684\u65f6\u5019\u7ed9\u6e05\u9664\u6389*\/\n    public T mView;\n\n    public void attach(T mView) {\n        this.mView = mView;\n    }\n\n    public void dettach() {\n        mView = null;\n    }\n\n}<\/code><\/pre>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public abstract class BaseMvpFragment&lt;V, T extends BasePresenter&lt;V&gt;&gt; extends Fragment {\n    \/*\u5b9a\u4e49\u4e00\u4e2aPresenter \u7528\u4e8e\u89e3\u7ed1\u6301\u6709\u7684View\n       *\n       * \u5728onAttach\u8fdb\u884c\u521d\u59cb\u5316Presenter\u7684\u64cd\u4f5c\n       * \u5728onCreate\u4e2d\u8fdb\u884c\u7ed1\u5b9a\n       * \u5728onDestroy \u4e2d\u8fdb\u884c\u89e3\u7ed1\n       *\n       **\/\n    public T mPresenter;\n\n    @Override\n    public void onAttach(Context context) {\n        super.onAttach(context);\n        mPresenter = initPresenter();\n    }\n\n    @Override\n    public void onCreate(@Nullable Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        mPresenter.attach((V) this);\n    }\n\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n        mPresenter.dettach();\n    }\n\n    \/* \u8fd9\u91cc\u662f\u9002\u914d\u4e3a\u4e0d\u540c\u7684View \u88c5\u8f7d\u4e0d\u540cPresenter*\/\n    public abstract T initPresenter();\n}<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u56db\u3001\u53c2\u8003\u5b66\u4e60<\/span><\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><a href=\"http:\/\/blog.csdn.net\/study_zhxu\/article\/details\/52152895\"> Android\u5355\u6392\u4e0a\u738b\u8005\u7cfb\u5217\u4e4bAndroid MVP\u89e3\u6790\u5b9e\u8df5<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"http:\/\/blog.csdn.net\/dantestones\/article\/details\/50899235\">Android mvp \u67b6\u6784\u7684\u81ea\u8ff0<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"http:\/\/blog.csdn.net\/hdszlk\/article\/details\/51367280\">Retrofit+MVP\u6a21\u5f0f\u5b66\u4e60<\/a><\/section>\n<\/li>\n<\/ul>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aMVP \u8ba9 Activity\/Fragment \u53ea\u505a View\uff0cPresenter \u5f53\u6865\uff0cModel \u7ba1\u6570\u636e\u548c\u8bf7\u6c42\uff1b\u57fa\u7c7b\u91cc attach\/detach\uff0c\u9875\u9762\u9500\u6bc1\u65f6\u628a View \u5f15\u7528\u6e05\u6389\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5e94\u7528\u5f00\u53d1\u603b\u4f1a\u78b0\u5230 UI\u3001\u6570\u636e\u3001\u4e1a\u52a1\u903b\u8f91\u3002\u903b\u8f91\u4e00\u590d\u6742\u5c31\u8981\u5206\u6846\u67b6\u3002\u6700\u65e9\u662f MVC\uff0c\u540e\u6765\u4e5f\u7528\u5728 Android \u4e0a\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[78],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1702"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1702"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":1719,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions\/1719"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}