一采章、需求分析
項目中的activity存在大量相同的方法和變量生命,且在activity的初始化方法中壶辜,也有部分代碼相同悯舟,為方便管理和使用activity,因此自定義一個適用自己的BaseActivity士复,在這里图谷,BaseActivity不包含任何頁面內(nèi)容翩活。
二、代碼實現(xiàn)
第一部分:BaseActivity
新建一個class名為“BaseActivity”便贵,內(nèi)容如下菠镇。代碼中已有注釋,這里不在說明承璃。
public abstract class BaseActivity< B extends ViewBinding > extends AppCompatActivity {
protected LoadingDialog loadDialog;
protected B view;
protected BaseActivity< B > activity;
protected Handler handler;
protected Bundle savedInstanceState;
protected final Map< String, IBasePresenter<?> > presenterMap = new HashMap<> ( );
@Override
@SuppressLint ( { "SourceLockedOrientationActivity" } )
protected void onCreate ( @Nullable Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
if ( userBaseOrientation ( ) ) {
if ( App.options.vertical ) {
//鎖定豎屏
setRequestedOrientation ( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
} else {
//鎖定橫屏
setRequestedOrientation ( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
}
} else {
Logger.i ( "不使用默認(rèn)方向鎖定功能" );
}
this.savedInstanceState = savedInstanceState;
this.activity = this;
this.handler = new Handler ( Looper.getMainLooper ( ) );
this.loadDialog = new LoadingDialog ( activity );
this.view = this.setBaseView ( );
if ( this.view != null ) {
if ( this.view.getRoot ( ).getLayoutParams ( ) == null ) {
this.view.getRoot ( ).setLayoutParams ( new LayoutParams ( - 1, - 1 ) );
}
this.setContentView ( this.view.getRoot ( ) );
}
this.onBaseCreate ( );
ListenerManager.addListener ( this );
}
public abstract B setBaseView ( );
public abstract void onBaseCreate ( );
public void addPresenter ( IBasePresenter< ? > presenter ) {
if ( presenter != null ) {
String name = presenter.getClass ( ).getSimpleName ( );
if ( ! TextUtils.isEmpty ( name ) ) {
this.presenterMap.put ( name, presenter );
}
}
}
protected boolean userBaseOrientation ( ) {
return true;
}
private void detachView ( ) {
for ( IBasePresenter< ? > presenter : this.presenterMap.values ( ) ) {
try {
presenter.detachView ( );
} catch ( Exception var4 ) {
var4.printStackTrace ( );
}
}
}
public void toast ( String s ) {
if ( ! TextUtils.isEmpty ( s ) ) {
Toast.makeText ( this.activity, s, Toast.LENGTH_SHORT ).show ( );
}
}
public void toastAndFinish ( String s ) {
if ( ! TextUtils.isEmpty ( s ) ) {
Toast.makeText ( this.activity, s, Toast.LENGTH_SHORT ).show ( );
}
this.finish ( );
}
public void showLoadingDialog ( String message ) {
loadDialog.setCancelable ( true );
loadDialog.show ( message );
}
public void showLoadingDialogFix ( String message ) {
loadDialog.setCancelable ( false );
loadDialog.show ( message );
}
public void closeLoadingDialog ( ) {
if ( loadDialog != null ) {
loadDialog.dismiss ( );
}
}
public void showLoadingDialog ( ) {
loadDialog.show ( null );
}
public void showLoadingDialogFinish ( String text,
DialogInterface.OnDismissListener listener ) {
LoadingDialog loadDialog = new LoadingDialog ( activity );
loadDialog.setCancelable ( true );
loadDialog.setCanceledOnTouchOutside ( false );
loadDialog.setOnDismissListener ( listener );
loadDialog.show ( text );
}
public void openActivity ( Class< ? > mClass ) {
this.startActivity ( new Intent ( this.activity, mClass ) );
}
public void openActivity ( Class< ? > mClass, int code ) {
this.startActivityForResult ( new Intent ( this.activity, mClass ), code );
}
public void openActivity ( Intent intent ) {
this.startActivity ( intent );
}
public void openActivity ( Intent intent, int code ) {
this.startActivityForResult ( intent, code );
}
public void hideKeyboard ( ) {
View view = this.getCurrentFocus ( );
if ( view != null ) {
InputMethodManager inputMethodManager =
( InputMethodManager ) this.getSystemService ( INPUT_METHOD_SERVICE );
inputMethodManager.hideSoftInputFromWindow ( view.getWindowToken ( ), 2 );
}
}
public void showKeyBoard ( View v ) {
InputMethodManager im =
( InputMethodManager ) this.getSystemService ( INPUT_METHOD_SERVICE );
im.showSoftInput ( v, 0 );
}
@Override
protected void onDestroy ( ) {
this.detachView ( );
this.handler.removeCallbacksAndMessages ( ( Object ) null );
ListenerManager.remove ( this );
super.onDestroy ( );
}
@Override
public void onActionListener ( int type, String s ) {
}
@Override
protected void onActivityResult ( int requestCode, int resultCode, @Nullable Intent data ) {
if ( - 1 == resultCode ) {
this.onActivityResultOk ( requestCode, data );
} else {
super.onActivityResult ( requestCode, resultCode, data );
}
}
protected void onActivityResultOk ( int requestCode, @Nullable Intent data ) {
}
public int color ( int resId ) {
return ContextCompat.getColor ( this.activity, resId );
}
public Drawable drawable ( int resId ) {
Drawable drawable = ContextCompat.getDrawable ( this.activity, resId );
if ( drawable != null ) {
drawable.setBounds ( 0, 0, drawable.getMinimumWidth ( ),
drawable.getMinimumHeight ( ) );
}
return drawable;
}
public String getIntentStr ( String key ) {
return this.activity.getIntent ( ).hasExtra ( key ) ?
this.activity.getIntent ( ).getStringExtra ( key ) : "";
}
public int getIntentInt ( String key ) {
return this.activity.getIntent ( ).hasExtra ( key ) ?
this.activity.getIntent ( ).getIntExtra ( key, 0 ) : - 1;
}
public boolean getIntentBoolean ( String key ) {
return this.activity.getIntent ( ).hasExtra ( key ) && this.activity.getIntent ( ).getBooleanExtra ( key, false );
}
public int getIntentInt ( String key, int defaultValue ) {
return this.activity.getIntent ( ).hasExtra ( key ) ?
this.activity.getIntent ( ).getIntExtra ( key, defaultValue ) : defaultValue;
}
public boolean checkIntentOk ( String... keys ) {
Intent intent = this.getIntent ( );
int var4 = keys.length;
for ( String key : keys ) {
if ( ! intent.hasExtra ( key ) ) {
return false;
}
}
return true;
}
public void setEmpty ( LinearLayout parant, String message ) {
if ( parant.getChildCount ( ) > 0 ) {
parant.removeAllViews ( );
}
if ( TextUtils.isEmpty ( message ) && App.options.noDataResId != 0 ) {
ImageView iv = new ImageView ( this );
iv.setLayoutParams ( new android.widget.LinearLayout.LayoutParams ( - 1,
SizeUt.getHeight ( 0.800000011920929D ) ) );
iv.setPadding ( 0, 20, 0, 20 );
iv.setImageResource ( App.options.noDataResId );
iv.setScaleType ( ScaleType.CENTER_INSIDE );
parant.addView ( iv );
} else {
message = TextUtils.isEmpty ( message ) ? "暫無數(shù)據(jù)" : message;
TextView tv = new TextView ( this );
tv.setLayoutParams ( new android.widget.LinearLayout.LayoutParams ( - 1,
SizeUt.getHeight ( 0.800000011920929D ) ) );
tv.setGravity ( 17 );
tv.setPadding ( 10, 50, 10, 50 );
tv.setTextColor ( - 16777216 );
tv.setText ( message );
parant.addView ( tv );
}
}
}
至此利耍,BaseActivity已經(jīng)建立完畢,接下來就是使用它盔粹。新建MainActivity(包括xml布局文件)隘梨。使用方法非常簡單。
public class MainActivity extends BaseActivity< ActivityMainBinding > {
@Override
public ActivityMainBinding setView ( ) {
return ActivityMainBinding.inflate ( getLayoutInflater ( ) );
}
@Override
public void onCreate ( ) {
view.tvTitle.setText("你好舷嗡,Android轴猎!");
}
}
MainActivity對應(yīng)的xml布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
第二部分:BaseFragment
新建一個class名為BaseFragment,內(nèi)容如下
public abstract class BaseFragment< B extends ViewBinding > extends Fragment {
protected LayoutInflater inflater;
protected ViewGroup container;
protected B view;
private LoadDialog loadDialog;
protected final Activity activity;
public BaseFragment ( Activity activity ) {
this.activity = activity;
}
@Nullable
@Override
public View onCreateView ( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState ) {
this.inflater = inflater;
this.container = container;
view = setView ( );
loadDialog = new LoadDialog ( activity );
onCreate ( );
return view.getRoot ( );
}
//忽略跟BaseActivity一樣的內(nèi)容
}
使用方法进萄,新建一個IndexFragment 繼承BaseFragment捻脖。
public class IndexFragment extends BaseFragment< FragmentIndexBinding > {
public IndexFragment ( Activity activity ) {
super ( activity );
}
@Override
public FragmentIndexBinding setView ( ) {
return FragmentIndexBinding.inflate ( inflater, container, true );
}
@Override
public void onCreate ( ) {
}
}
三、注意事項
1中鼠、以上代碼實現(xiàn)可婶,請使用viewBinding。
android {
······
buildFeatures {
viewBinding = true
}
}
2援雇、base可以預(yù)先定義一個容器矛渴,如統(tǒng)一的標(biāo)題,返回鍵等惫搏。獲取到子頁面的viewbinding后具温,用view.addView();方法即可。如遇到layoutParams為空的情況筐赔,請在base中做處理桂躏。
3、BaseFragment為避免activity為空川陆,這里直接使用mainActivity。
四蛮位、總結(jié)
1较沪、通過以上方式建立的BaseActivity和BaseFragment,相對而言失仁,代碼量少很多尸曼。而且base中聲明的方法很明確不容易出錯。不像別的方法如init(),main();doWork();doStart();容易混淆萄焦,稍有不慎可能還會導(dǎo)致空指針控轿。
2冤竹、ViewBinding為android程序猿節(jié)省了不少時間,新項目或在條件允許下茬射,建議更換成它鹦蠕。