版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】AndroidMVP中如何實現(xiàn)BaseFragment通用式封裝
/upload/information/20200623/125/117889.pngpackage
com.test.mvp.mvpdemo.mvp.v6.basemvp;
import
android.os.Bundle;
import
android.support.annotation.IdRes;
import
android.support.annotation.LayoutRes;
import
android.support.annotation.NonNull;
import
android.support.annotation.Nullable;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
com.test.mvp.mvpdemo.mvp.v6.inject.InjectPresenter;
import
java.lang.reflect.Field;
import
java.util.ArrayList;
import
java.util.List;
public
abstract
class
BaseFragment
extends
Fragment
implements
IBaseView
{
private
List<BasePresenter>
mInjectPresenters;
private
View
mLayoutView;
protected
abstract
@LayoutRes
int
setLayout();
protected
abstract
void
initViews(@Nullable
Bundle
savedInstanceState);
protected
abstract
void
initData();
@SuppressWarnings("ConstantConditions")
protected
<T
extends
View>
T
$(@IdRes
int
viewId)
{
return
this.getView().findViewById(viewId);
}
@SuppressWarnings({"unchecked",
"TryWithIdenticalCatches"})
@Nullable
@Override
public
View
onCreateView(@NonNull
LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
View
view
=
inflater.inflate(setLayout(),
container,
false);
mInjectPresenters
=
new
ArrayList<>();
//獲得已經(jīng)申明的變量,包括私有的
Field[]
fields
=
this.getClass().getDeclaredFields();
for
(Field
field
:
fields)
{
//獲取變量上面的注解類型
InjectPresenter
injectPresenter
=
field.getAnnotation(InjectPresenter.class);
if
(injectPresenter
!=
null)
{
try
{
Class<?
extends
BasePresenter>
type
=
(Class<?
extends
BasePresenter>)
field.getType();
BasePresenter
mInjectPresenter
=
type.newInstance();
//綁定
mInjectPresenter.attach(this);
field.setAccessible(true);
field.set(this,
mInjectPresenter);
mInjectPresenters.add(mInjectPresenter);
}
catch
(IllegalAccessException
e)
{
e.printStackTrace();
}
catch
(java.lang.InstantiationException
e)
{
e.printStackTrace();
}
catch
(ClassCastException
e)
{
e.printStackTrace();
throw
new
RuntimeException("SubClass
must
extends
Class:BasePresenter");
}
}
}
return
view;
}
@Override
public
void
onViewCreated(@NonNull
View
view,
@Nullable
Bundle
savedInstanceState)
{
super.onViewCreated(view,
savedInstanceState);
initViews(savedInstanceState);
initData();
}
@Override
public
void
onDestroy()
{
super.onDestroy();
for
(BasePresenter
presenter
:
mInjectPresenters)
{
presenter.detach();
}
mInjectPresenters.clear();
mInjectPresenters
=
null;
}
}/upload/information/20200623/125/117890.pngpublic
class
SecondActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(@Nullable
Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/**
*
開啟一個
fragment
*/
getSupportFragmentManager().beginTransaction().replace(R.id.second_container,
new
SecondFragment()).commit();
}
}<?xml
version="1.0"
encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mvc.MainActivity">
<FrameLayout
android:id="@+id/second_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>View層:新建SecondFragment實現(xiàn)類:package
com.test.mvp.mvpdemo.mvp.v6.view;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.test.mvp.mvpdemo.R;
import
com.test.mvp.mvpdemo.mvp.v6.SecondContract;
import
com.test.mvp.mvpdemo.mvp.v6.basemvp.BaseFragment;
import
com.test.mvp.mvpdemo.mvp.v6.inject.InjectPresenter;
import
com.test.mvp.mvpdemo.mvp.v6.presenter.SecondPresenter;
public
class
SecondFragment
extends
BaseFragment
implements
SecondContract.ISecondView
{
private
TextView
tvFragment;
@InjectPresenter
private
SecondPresenter
mPresenter;
@Override
protected
int
setLayout()
{
return
R.layout.fragment_second;
}
@Override
protected
void
initViews(@Nullable
Bundle
savedInstanceState)
{
tvFragment
=
$(R.id.tv_fragment);
}
@Override
protected
void
initData()
{
mPresenter.handlerData();
}
@Override
public
void
showDialog()
{
//
Toast.makeText(getContext(),
"this
is
Fragment",
Toast.LENGTH_SHORT).show();
}
@SuppressWarnings("ConstantConditions")
@Override
public
void
succes(String
content)
{
getActivity().runOnUiThread(new
Runnable()
{
@Override
public
void
run()
{
Toast.makeText(getContext(),
""
+
content,
Toast.LENGTH_SHORT).show();
tvFragment.setText(content);
}
});
}
}###Presenter
層:新建
SecondPresenter
實現(xiàn)類:
package
com.test.mvp.mvpdemo.mvp.v6.presenter;
import
com.test.mvp.mvpdemo.mvp.v6.SecondContract;
import
com.test.mvp.mvpdemo.mvp.v6.basemvp.BasePresenter;
import
com.test.mvp.mvpdemo.mvp.v6.model.SecondModel;
import
java.io.IOException;
import
okhttp3.Call;
import
okhttp3.Callback;
import
okhttp3.Response;
public
class
SecondPresenter
extends
BasePresenter<SecondContract.ISecondView,
SecondModel>
implements
SecondContract.ISecondPresenter
{
@Override
public
void
handlerData()
{
getView().showDialog();
getModel().requestBaidu(new
Callback()
{
@Override
public
void
onFailure(Call
call,
IOException
e)
{
}
@Override
public
void
onResponse(Call
call,
Response
response)
throws
IOException
{
String
content
=
response.body().string();
getView().succes(content);
}
});
}
}package
com.test.mvp.mvpdemo.mvp.v6.model;
import
com.test.mvp.mvpdemo.mvp.v6.SecondContract;
import
com.test.mvp.mvpdemo.mvp.v6.basemvp.BaseModel;
import
okhttp3.Callback;
import
okhttp3.OkHttpClient;
import
okhttp3.Request;
public
class
SecondModel
extends
BaseModel
implements
SecondContract.ISecondModel
{
@Override
public
void
requestBaidu(Callback
callback)
{
OkHttpClient
client
=
new
OkHttpClient();
Request
request
=
new
Request.Builder()
.url("/smile_running")
.build();
client.newCall(request).enqueue(callback);
}
}package
com.test.mvp.mvpdemo.mvp.v6;
import
com.test.mvp.mvpdemo.mvp.v6.basemvp.IBasePresenter;
import
com.test.mvp.mvpdemo.mvp.v6.basemvp.IBaseView;
import
okhttp3.Callback;
public
interface
SecondContract
{
interface
ISecondModel
{
void
requestBaidu(Callback
callback);
}
interface
ISecondView
extends
IBaseView
{
void
showDialog();
void
succes(String
content);
}
interface
ISecondPresenter
extends
IBasePresenter
{
void
handlerData();
}
}/upload/information/20200623/125/117891.png記錄錯誤原因:在子線程中更新UI操作。
@Override
public
void
succes(String
content)
{
Toast.makeText(getContext(),
""
+
content,
Toast.LENGTH_SHORT).show();
tvFragment.setText(content);
}07-09
23:51:21.887
9769-9788/com.test.mvp.mvpdemo
E/EGL_adreno:
tid
9788:
eglSurfaceAttrib(1319):
error
0x3009
(EGL_BAD_MATCH)
07-09
23:51:21.915
9769-9788/com.test.mvp.mvpdemo
E/EGL_adreno:
tid
9788:
eglSurfaceAttrib(1319):
error
0x3009
(EGL_BAD_MATCH)
07-09
23:51:23.362
9769-9788/com.test.mvp.mvpdemo
E/EGL_adreno:
tid
9788:
eglSurfaceAttrib(1319):
error
0x3009
(EGL_BAD_MATCH)
07-09
23:51:27.742
9769-9788/com.test.mvp.mvpdemo
E/EGL_adreno:
tid
9788:
eglSurfaceAttrib(1319):
error
0x3009
(EGL_BAD_MATCH)
07-09
23:51:28.069
9769-9798/com.test.mvp.mvpdemo
E/AndroidRuntime:
FATAL
EXCEPTION:
OkHttp
Dispatcher
Process:
com.test.mvp.mvpdemo,
PID:
9769
java.lang.reflect.UndeclaredThrowableException
at
$Proxy2.succes(Unknown
Source)
at
com.test.mvp.mvpdemo.mvp.v6.presenter.SecondPresenter$1.onResponse(SecondPresenter.java:25)
at
okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at
java.lang.Thread.run(Thread.java:818)
Caused
by:
java.lang.reflect.InvocationTargetException
at
java.lang.reflect.Method.invoke(Native
Method)
at
java.lang.reflect.Method.invoke(Method.java:372)
at
com.test.mvp.mvpdemo.mvp.v6.basemvp.BasePresenter$1.invoke(BasePresenter.java:31)
at
java.lang.reflect.Proxy.invoke(Proxy.java:397)
at
$Proxy2.succes(Unknown
Source)
at
com.test.mvp.mvpdemo.mvp.v6.presenter.SecondPresenter$1.onResponse(SecondPresenter.java:25)
at
okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at
java.lang.Thread.run(Thread.java:818)
Caused
by:
java.lang.RuntimeException:
Can't
create
handler
inside
thread
that
has
not
called
Looper.prepare()
at
android.os.Handler.<init>(Handler.java:200)
at
android.os.Handler.<init>(Handler.java:114)
at
android.widget.Toast$TN.<init>(Toast.java:359)
at
android.widget.Toast.<init>(Toast.ja
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度公司股東內(nèi)部關于企業(yè)社會責任履行共識協(xié)議3篇
- 二零二五農(nóng)村合作建房工程招投標及合同管理協(xié)議
- 二零二五年度環(huán)保設施項目公司轉讓合同3篇
- 2025年度農(nóng)村公路養(yǎng)護與社區(qū)文化活動合同2篇
- 2025年度外賣配送公司送餐服務優(yōu)化合同3篇
- 2025年度公司與公司簽訂的智慧城市建設合作協(xié)議3篇
- 2025年度綠色養(yǎng)殖產(chǎn)業(yè)鏈合作協(xié)議書-養(yǎng)羊篇3篇
- 2025年度公司車輛充電設施建設及使用協(xié)議3篇
- 二零二五年度特色水果種植基地果園土地承包合同3篇
- 2025年度農(nóng)村土地流轉承包合同(農(nóng)產(chǎn)品品牌推廣)
- 遼寧省撫順市撫順縣2023-2024學年八年級上學期期末考試數(shù)學試卷(含解析)
- 《中國武術的起源》課件
- 期末測試卷(試題)(含答案)2024-2025學年北師大版數(shù)學五年級上冊
- 2024年道路運輸安全生產(chǎn)管理制度樣本(5篇)
- 2024年度股權轉讓合同標的及受讓方條件
- 起重機司機-特種設備操作Q2證考試練習題(含答案)
- 2024年《瘧疾防治知識》課件
- 礦業(yè)數(shù)字化轉型研究
- 《現(xiàn)代控制理論》全套課件(東北大學)
- 2024春季中鐵三局集團校園招聘高頻難、易錯點500題模擬試題附帶答案詳解
- 出版社圖書編輯出版流程規(guī)范
評論
0/150
提交評論