安卓

Android 自定义BaseActivity,BaseFrag

2021-04-16  本文已影响0人  刘坤林

一、需求分析

项目中的activity存在大量相同的方法和变量生命,且在activity的初始化方法中,也有部分代码相同,为方便管理和使用activity,因此自定义一个适用自己的BaseActivity,在这里,BaseActivity不包含任何页面内容。

二、代码实现

第一部分:BaseActivity

新建一个class名为“BaseActivity”,内容如下。代码中已有注释,这里不在说明。

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 ( "不使用默认方向锁定功能" );
        }
        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 ) ? "暂无数据" : 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已经建立完毕,接下来就是使用它。新建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对应的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,内容如下

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一样的内容
}

使用方法,新建一个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、以上代码实现,请使用viewBinding。

android {
    ······
    buildFeatures {
        viewBinding = true
    }
}

2、base可以预先定义一个容器,如统一的标题,返回键等。获取到子页面的viewbinding后,用view.addView();方法即可。如遇到layoutParams为空的情况,请在base中做处理。
3、BaseFragment为避免activity为空,这里直接使用mainActivity。

四、总结

1、通过以上方式建立的BaseActivity和BaseFragment,相对而言,代码量少很多。而且base中声明的方法很明确不容易出错。不像别的方法如init(),main();doWork();doStart();容易混淆,稍有不慎可能还会导致空指针。
2、ViewBinding为android程序猿节省了不少时间,新项目或在条件允许下,建议更换成它。

上一篇下一篇

猜你喜欢

热点阅读