android activity自定义抽象父类
2017-07-20 18:22
323 查看
android开发最常用的组件就是activity,但是activity中也有很多常用的方法,几乎每次建立activity后都需要调用的一些方法流程,在此自定义一个父类-BaseActivity,使所有的activity都继承于这个父类,继承以后会自动继承父类的方法,并集成了一些界面跳转动画等公共效果,
BaseActivity.java
activity_base_layout.xml
not_exit_push_left_in.xml
not_exit_push_left_out.xml
push_right_in.xml
push_right_out.xml
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity { protected Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(); setContentView(R.layout.activity_base_layout); initParent(); findViews(); setListensers(); MyApplication myApplication = (MyApplication) getApplication(); myApplication.addTempActivityInBackStack(this); } @Override protected void onActivityResult(int arg0, int arg1, Intent arg2) { // TODO Auto-generated method stub super.onActivityResult(arg0, arg1, arg2); } private void initParent() { mContext = this; LinearLayout subCententView = (LinearLayout) this.findViewById(R.id.base_sub_activty_layout); LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); View centerView = View.inflate(mContext, setContentView(), null); subCententView.addView(centerView, layoutParams); } protected boolean isShowNoNetworksPrompt() { return true; } /** * 跳转到某个Activity */ protected void gotoActivity(Context mContext, Class<?> toActivityClass, Bundle bundle) { Intent intent = new Intent(mContext, toActivityClass); if (bundle != null) { intent.putExtras(bundle); } mContext.startActivity(intent); ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);//页面跳转动画 } /** * 退出到某个Activity */ protected void backActivity() { finish(); overridePendingTransition(R.anim.not_exit_push_left_in, R.anim.push_right_out);//页面退出动画 } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 所有需要统一处理的onKeyDown写在这个if里面 if (isOnKeyDown()) { if (keyCode == KeyEvent.KEYCODE_BACK) { backActivity(); } } return super.onKeyDown(keyCode, event); } protected boolean isOnKeyDown() { return true; } /** * 加载子类布局 */ protected abstract int setContentView(); /** * 加载控件 */ protected abstract void findViews(); /** * 设置监听 */ protected abstract void setListensers(); @Override protected void onResume() { super.onResume(); MyApplication myApplication = (MyApplication) getApplication(); myApplication.setResumeContext(this); if (JPushInterface.isPushStopped(getApplicationContext())) { JPushInterface.resumePush(getApplicationContext()); } } }
activity_base_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/base_sub_activty_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:orientation="vertical" > </LinearLayout> </RelativeLayout>
not_exit_push_left_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0%p" android:interpolator="@android:anim/linear_interpolator" android:toXDelta="0%p" /> </set>
not_exit_push_left_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0%p" android:interpolator="@android:anim/linear_interpolator" android:toXDelta="0%p" /> </set>
push_right_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="100%p" android:interpolator="@android:anim/linear_interpolator" android:toXDelta="0%p" /> </set>
push_right_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0%p" android:interpolator="@android:anim/linear_interpolator" android:toXDelta="100%p" /> </set>
相关文章推荐
- 自定义fragment抽象父类
- android activity自定义抽象父类
- 子类实现父类的抽象方法,父类调用抽象方法
- 通过包装父类的抽象方法达到结构和行为都统一
- 子类实现父类的抽象方法,父类调用抽象方法
- Android自定义UI陷阱:LayoutInflater.from().inflate()一定不能工作在父类或虚类里
- App9_07_自定义异常类 以Exception为直接父类
- 子类实现父类的抽象方法,父类调用抽象方法
- Android自定义适配器父类继承BaseAdapter,自定义生成对话框工具类
- 1019MFC中自定义函数想要调用父类控件的解决方法
- Xamarin XAML语言教程ContentView视图作为自定义视图的父类
- 记录自学Android的笔记-自定义一个ActivityCollector类来管理所有活动和自定义BaseActivity作为所有活动的父类
- 子类实现父类的抽象方法,父类调用抽象方法
- 通过包装父类的抽象方法达到结构和行为都统一
- Xamarin XAML语言教程ContentView视图作为自定义视图的父类
- 方法重写,抽象方法必须重写,抽象类。super.父类里面的方法调用方法。
- 子类实现父类的抽象方法,父类调用抽象方法
- 抽象父类SqlHelper
- Android studio快捷键重写继承父类的抽象方法
- Netty 解码器抽象父类 ByteToMessageDecoder 源码解析