两种构建具有ToolBar的BaseActivity的方式
两种构建具有ToolBar的BaseActivity的方式
本着挤一点写一点的原则,写下这篇博文,一直都有农友在封装BaseActivity的路上越陷越深,我觉得没有必要纠结你BaseActivity的完善性,本着挤一点写一点的原则就行了,这里我用了两种方式来实现BaseActivity,一种是常用的继承方式,令一种是在构建app的activity管理长效机制的时候动态的加入我们为每一个activity的配置,各有千秋,看您的喜好吧。
- Annotation的使用
- 基础配置
- 传统方式
- ActivityLifecycleCallbacks方式
- 效果图
Annotation
自定义注解的实现,须要了解Java为我们提供的元注解和相关定义注解的语法,可以用这个手写Butternife
aaa@qq.com,
aaa@qq.com,
aaa@qq.com,
aaa@qq.com
这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。
@Target:
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
使用:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LayoutId {
int value() default 0;
}
@Retention:
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效),这里我们大部分使用RUNTIME,如果要手写Butternife,要用class,并且用代理对注解进行编译时的类创建
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)
Retention meta-annotation类型有唯一的value作为成员,它的取值来自java.lang.annotation.RetentionPolicy的枚举类型值。如实现Butternife部分实现如下:
public class ZhsViewBinder {
private static final ActivityViewFinder act_viewFinder=new ActivityViewFinder();
private static final FragmentViewFinder frg_viewFinder=new FragmentViewFinder();
private static final HashMap<String,ViewBinder> binderMap=new HashMap<>();
public static void bind(Activity activity){
bind(activity,activity,act_viewFinder);
}
public static void bind(Fragment fragment){
bind(fragment,fragment,frg_viewFinder);
}
private static void bind(Object host,Object object,ViewFinder finder){
String className=host.getClass().getName();
try {
ViewBinder viewBinder=binderMap.get(className);
if(viewBinder==null){
Class<?> aclass=Class.forName(className+ "$$ViewBinder");
viewBinder= (ViewBinder) aclass.newInstance();
binderMap.put(className,viewBinder);
}
if(viewBinder!=null){
viewBinder.bindView(host,object,finder);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void unBind(Object host){
String className=host.getClass().getName();
ViewBinder viewBinder=binderMap.get(className);
if(viewBinder!=null){
viewBinder.unBind(host);
}
binderMap.remove(className);
}
}
/**
* ui提供者
*/
public interface ViewFinder {
View findView(Object object,int id);
}
/**
* 接口绑定
* @param <T>
*/
public interface ViewBinder<T> {
void bindView(T host,Object object,ViewFinder finder);
void unBind(T host);
}
/**
* activity 的入口
*/
public class ActivityViewFinder implements ViewFinder{
@Override
public View findView(Object object, int id) {
return ((Activity)object).findViewById(id);
}
}
/**
* activity 的使用
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
public @interface BindView {
int value();
}
在activity中即可这样使用
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_id)
TextView textView;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ZhsViewBinder.bind(this);//注意这个要在setContentView之后调用,在baseactivity中使用也是一样
}
}
@Documented:
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
@Inherited:
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
@Inherited
public @interface BindView{
public enum ViewText { DEFAULT,WAIT,BLACK};
String name();
ViewText test() default FontColor.DEFAULT;
}
灵活运用以上即可达到很好的效果
ToolBar布局
注意使用自定义的ToolBar,将acativity的主题设置为NoActionBar,我这里的布局没有写的很好看,自己根据设计稿来优化
设置NoActionBar,
Theme下的
TOOLBAR布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/toolbar"
app:contentInsetStart="0dp"
android:layout_height="48dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<RelativeLayout
android:layout_width="48dp"
android:id="@+id/base_left_rl_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
android:src="@mipmap/back_defaut"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/base_left_img"
android:visibility="gone"
/>
<TextView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:text="返回"
android:id="@+id/base_left_tv"
android:layout_marginLeft="10dp"
android:visibility="gone"
android:gravity="center"/>
</RelativeLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="48dp"
android:gravity="center"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:id="@+id/base_center_ll_content"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
android:src="@mipmap/back_defaut"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="@+id/base_center_img"
/>
<TextView
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:text="创建累的色是"
android:visibility="gone"
android:id="@+id/base_center_tv"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/base_right_rl_content"
android:layout_alignParentRight="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
android:visibility="gone"
android:src="@mipmap/back_defaut"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:id="@+id/base_right_img"
/>
<TextView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:text="返回"
android:visibility="gone"
android:id="@+id/base_right_tv"
android:layout_marginRight="10dp"
android:gravity="center"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="1dp"
android:id="@+id/base_bottom_line"
android:background="#8a8a8a"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
以后为每一个BaseActivity设置
<include layout="@layout/basetoolbar_layout"></include>
为ToolBar配置Annotation和为Activity配置一个布局id的注解
//toolbar的中间图片的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CenterImag {
int value() default 0;
}
//activity的布局id的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LayoutId {
int value() default 0;
}
//toolbar的左边图片的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LeftImag {
int value() default 0;
}
//toolbar的左边文字的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LeftText {
String value() default "";
}
//toolbar的右边图片的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RightImag {
int value() default 0;
}
//toolbar的右边文字的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RightText {
String value() default "";
}
//toolbar的中间文字的注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TitleText {
String value() default "";
}
是不是很简单,过会再提他的具体使用,先来说一下实现BaseActivity
BaseActivity封装方式一
本着挤一点写一点的原则,我仿照Recovery写了一个crash框架,发现它里面利用了ActivityLifecycleCallbacks来实现对Activity的恢复记录以及相关的操作,我才发现这个东西的用处很大,感谢郑晓勇大神
-
第一步手写一个类实现ActivityLifecycleCallbacks
UiReinstateActivityLifecycleCallback
/**
* Created by zhanghs on 2017/11/21/021.
* crash 的初始化以及对activity的butternife的初始化
* 这里需要activity 实现一个接口UiActivityAnnitation,就会自动为实现接口的activity增加toolbar
* 或者使用baseactivity,
*/
public class UiReinstateActivityLifecycleCallback implements Application.ActivityLifecycleCallbacks {
public TextView tvCenter, tvLeft, tvRight;
public ImageView imgCenter, imgLeft, imgRight;
public RelativeLayout rlLeft, rlRight;
public LinearLayout llCenter;
private Unbinder unbinder; //butternife的配置
@Override
public void onActivityCreated(final Activity activity, Bundle bundle) {
Class mClass = activity.getClass();
//判断该activity是否实现了UiActivityAnnitation接口,如果是我们才给她加toolbar
//为的是一些特殊的activity不需要实现toolbar的我们可以跳过
if (activity instanceof UiActivityAnnitation) {
//一下均为对annotation的获取,是不是特别甘醇
LayoutId layoutId = (LayoutId) mClass.getAnnotation(LayoutId.class);
if (layoutId != null && layoutId.value() != 0) {
activity.setContentView(layoutId.value());
}
unbinder = ButterKnife.bind(activity);
if (activity.findViewById(R.id.toolbar) != null) { //找到 Toolbar 并且替换 Actionbar
if (activity instanceof AppCompatActivity) {
((AppCompatActivity) activity).setSupportActionBar((Toolbar) activity.findViewById(R.id.toolbar));
((AppCompatActivity) activity).getSupportActionBar().setDisplayShowTitleEnabled(false);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.setActionBar((android.widget.Toolbar) activity.findViewById(R.id.toolbar));
activity.getActionBar().setDisplayShowTitleEnabled(false);
}
}
tvCenter = activity.findViewById(R.id.base_center_tv);
tvLeft = activity.findViewById(R.id.base_left_tv);
tvRight = activity.findViewById(R.id.base_right_tv);
imgLeft = activity.findViewById(R.id.base_left_img);
imgCenter = activity.findViewById(R.id.base_center_img);
imgRight = activity.findViewById(R.id.base_right_img);
rlLeft = activity.findViewById(R.id.base_left_rl_content);
rlRight = activity.findViewById(R.id.base_right_rl_content);
llCenter = activity.findViewById(R.id.base_center_ll_content);
LeftImag leftImag = (LeftImag) mClass.getAnnotation(LeftImag.class);
LeftText leftText = (LeftText) mClass.getAnnotation(LeftText.class);
TitleText titleText = (TitleText) mClass.getAnnotation(TitleText.class);
CenterImag centerImag = (CenterImag) mClass.getAnnotation(CenterImag.class);
RightImag rightImag = (RightImag) mClass.getAnnotation(RightImag.class);
RightText rightText = (RightText) mClass.getAnnotation(RightText.class);
if (titleText != null && titleText.value().length() != 0) {
tvCenter.setVisibility(View.VISIBLE);
tvCenter.setText(titleText.value());
}
if (leftText != null && leftText.value().length() != 0) {
tvLeft.setVisibility(View.VISIBLE);
tvLeft.setText(leftText.value());
}
if (rightText != null && rightText.value().length() != 0) {
tvRight.setVisibility(View.VISIBLE);
tvRight.setText(rightText.value());
}
if (leftImag != null && leftImag.value() != 0) {
imgLeft.setVisibility(View.VISIBLE);
imgLeft.setImageResource(leftImag.value());
}
if (rightImag != null && rightImag.value() != 0) {
imgRight.setVisibility(View.VISIBLE);
imgRight.setImageResource(rightImag.value());
}
if (centerImag != null && centerImag.value() != 0) {
imgCenter.setVisibility(View.VISIBLE);
imgCenter.setImageResource(centerImag.value());
}
rlLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
activity.onBackPressed();
}
});
}
//将这个callback回掉回去,既可以满足每个activity的初始化,又可以对toolbar的控件进行相关操作
((UiActivityAnnitation) activity).initView(this);
((UiActivityAnnitation) activity).initEvent(this);
}
}
@Override
public void onActivityStarted(final Activity activity) {
//注意这里是我对crash的封装,你们可以删除掉一下代码
//判断是否是应该跳过的activity或者是crash的显示activity
boolean isLegal = ReinstateStore.getInstance().verifyActivity(activity);
if (!isLegal)
return;
if (activity.getIntent().getBooleanExtra(ReinstateActivity.RECOVERY_MODE_ACTIVE, false)) {
Reflect.on(Reinstate.class).method("registerRecoveryProxy").invoke(Reinstate.getInstance());
}
if (ReinstateStore.getInstance().contains(activity))
return;
Window window = activity.getWindow();
if (window != null) {
View decorView = window.getDecorView();
if (decorView == null)
return;
decorView.post(new Runnable() {
@Override
public void run() {
ReinstateStore.getInstance().putActivity(activity);
Object o = activity.getIntent().clone();
ReinstateStore.getInstance().setIntent((Intent) o);
}
});
}
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
ReinstateStore.getInstance().removeActivity(activity);
if(unbinder!=null){
unbinder.unbind();
unbinder=null;
}
}
}
//这里的UiActivityAnnitation就是变相的让Activity成为BaseActivity
/**
* Created by zhanghs on 2017/11/21/021.
* 对注解的提示,以及对ui的封装
*/
public interface UiActivityAnnitation {
void initView(UiReinstateActivityLifecycleCallback callback);
void initEvent(UiReinstateActivityLifecycleCallback callback);
}
配置一个全局的Application对UiReinstateActivityLifecycleCallback初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new UiReinstateActivityLifecycleCallback());
}
}
//别忘了在清单文件中配置这个Application
android:name=".App"
Activity的使用
@LayoutId(R.layout.activity_test1)
@TitleText("第二页")
@LeftText("返回")
@RightText("你好啊1")
//由于是implements 可以使activity不在因为单继承BaseActivity而带来很多麻烦
public class TestActivity1 extends AppCompatActivity implements UiActivityAnnitation{
@BindView(R.id.make)
Button make;
@BindView(R.id.next)
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@OnClick({R.id.make,R.id.next})
public void onClick(View view){
switch (view.getId()){
case R.id.make:
System.out.println(1/0);
break;
case R.id.next:
startActivity(new Intent(this,TestActivity2.class));
break;
}
}
//回掉回来可以对toolbar的所有控件进行设置与操作,嘎嘣脆
@Override
public void initView(UiReinstateActivityLifecycleCallback callback) {
callback.tvCenter.setText("哈哈哈 我回来了");
}
@Override
public void initEvent(UiReinstateActivityLifecycleCallback callback) {
}
}
效果图,不要在意一些细节- -、
其他的一些配置就看你想怎么玩了
BaseActivity封装方式二
传统的BaseActivity的书写方式也游一遍,这里我只简单的封装了一个BaseHandler,为了防止一直占有Activity的引用,造成泄露
BaseHander
/**
* Created by zhanghs on 2017/11/21/021.
* 防止内存泄漏的handler
*/
public abstract class BaseHandler extends Handler {
protected WeakReference<Activity> activityWeakReference;
protected WeakReference<Fragment> fragmentWeakReference;
/** 标记异步操作返回时目标界面已经消失时的处理状态 */
public static final int ACTIVITY_GONE = -1;
protected BaseHandler(){}
public BaseHandler(Activity activity){
this.activityWeakReference=new WeakReference<Activity>(activity);
}
public BaseHandler(Fragment fragment){
this.fragmentWeakReference=new WeakReference<Fragment>(fragment);
}
@Override
public void handleMessage(Message msg) {
if (activityWeakReference != null&& activityWeakReference.get() != null&& (!activityWeakReference.get().isFinishing())) {
// 确认Activity是否可用
handleMessage(msg, msg.what);
return;
} else if(fragmentWeakReference != null && fragmentWeakReference.get() != null &&(! fragmentWeakReference.get().isRemoving())){
handleMessage(msg, msg.what);
return;
}else {
handleMessage(msg,ACTIVITY_GONE);
}
}
/**
* 抽象方法用户实现,用来处理具体的业务逻辑
*
* @param msg
* @param what
*/
public abstract void handleMessage(Message msg, int what);
}
具体的BaseActivity
//实现对EventBus的封装
public interface BaseEventEnter {
void registerEvent();
void unregisterEvent();
}
//BaseActivity
public abstract class BaseActivity<T> extends AppCompatActivity implements View.OnClickListener, BaseEventEnter {
private TextView tvCenter, tvLeft, tvRight;
private ImageView imgCenter, imgLeft, imgRight;
private RelativeLayout rlLeft, rlRight;
private LinearLayout llCenter;
private Toolbar toolbar;
private Unbinder unbinder;
protected static BaseHandler baseHandler ;
protected Context bContext;
protected LayoutInflater bInflater;
protected View bContentView;
protected HttpManager bApi;
protected RequestManager bGlide;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
baseHandler=new MyHandler(this) {
@Override
public void handleMessage(Message msg, int what) {
handleMessageThis(msg,what);
}
};
bContext=this;
bInflater=LayoutInflater.from(this);
bApi=HttpManager.init(this);
bGlide=Glide.with(this);
LayoutId layoutId = getClass().getAnnotation(LayoutId.class);
if (layoutId != null && layoutId.value() != 0) {
setContentView(layoutId.value());
} else {
setContentView(contentLayoutId());
}
}
public static abstract class MyHandler extends BaseHandler{
public MyHandler(Activity activity) {
super(activity);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterEvent();
if(unbinder!=null){
unbinder.unbind();
unbinder=null;
}
}
public void handleMessageThis(Message msg, int what){};
public void setContentView(@LayoutRes int layoutResID) {
if(layoutResID==0)return;
bContentView=bInflater.inflate(layoutResID,null);
if(bContentView==null)return;
super.setContentView(bContentView);
registerEvent();
//这里保存unbinder 千万别用Serializable的序列化
unbinder=ButterKnife.bind(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null){
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
tvCenter = (TextView) findViewById(R.id.base_center_tv);
tvLeft = (TextView) findViewById(R.id.base_left_tv);
tvRight = (TextView) findViewById(R.id.base_right_tv);
imgLeft = (ImageView) findViewById(R.id.base_left_img);
imgCenter = (ImageView) findViewById(R.id.base_center_img);
imgRight = (ImageView) findViewById(R.id.base_right_img);
rlLeft = (RelativeLayout) findViewById(R.id.base_left_rl_content);
rlRight = (RelativeLayout) findViewById(R.id.base_right_rl_content);
llCenter = (LinearLayout) findViewById(R.id.base_center_ll_content);
Class mClass = getClass();
LeftImag leftImag = (LeftImag) mClass.getAnnotation(LeftImag.class);
LeftText leftText = (LeftText) mClass.getAnnotation(LeftText.class);
TitleText titleText = (TitleText) mClass.getAnnotation(TitleText.class);
CenterImag centerImag = (CenterImag) mClass.getAnnotation(CenterImag.class);
RightImag rightImag = (RightImag) mClass.getAnnotation(RightImag.class);
RightText rightText = (RightText) mClass.getAnnotation(RightText.class);
if (titleText != null && titleText.value().length() != 0) {
tvCenter.setVisibility(View.VISIBLE);
tvCenter.setText(titleText.value());
}
if (leftText != null && leftText.value().length() != 0) {
tvLeft.setVisibility(View.VISIBLE);
tvLeft.setText(leftText.value());
}
if (rightText != null && rightText.value().length() != 0) {
tvRight.setVisibility(View.VISIBLE);
tvRight.setText(rightText.value());
}
if (leftImag != null && leftImag.value() != 0) {
imgLeft.setVisibility(View.VISIBLE);
imgLeft.setImageResource(leftImag.value());
}
if (rightImag != null && rightImag.value() != 0) {
imgRight.setVisibility(View.VISIBLE);
imgRight.setImageResource(rightImag.value());
}
if (centerImag != null && centerImag.value() != 0) {
imgCenter.setVisibility(View.VISIBLE);
imgCenter.setImageResource(centerImag.value());
}
rlLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
}
}
@Override
public void registerEvent() {
EventBus.getDefault().register(this);
}
@Override
public void unregisterEvent() {
EventBus.getDefault().unregister(this);
}
public abstract void initView();
public abstract void initEvent();
protected int contentLayoutId(){return 0;}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onDataSynEvent(BaseEventBean<T> event) {
onDataEvent(event);
}
protected void onDataEvent(BaseEventBean<T> event){
}
/**
* 中间的点击事件
*
* @param onClickListener
*/
public void setCenterClick(View.OnClickListener onClickListener) {
llCenter.setOnClickListener(onClickListener);
}
/**
* 左边的点击事件
*
* @param onClickListener
*/
public void setLeftClick(View.OnClickListener onClickListener) {
rlLeft.setOnClickListener(onClickListener);
}
/**
* 右边的点击事件
*
* @param onClickListener
*/
public void setRightClick(View.OnClickListener onClickListener) {
rlRight.setOnClickListener(onClickListener);
}
/**
* 左边的文字
*
* @param text
*/
public void setTvLeftText(String text) {
tvLeft.setText(text);
}
/**
* 中间的文字
*
* @param text
*/
public void setTvCenterText(String text) {
tvCenter.setText(text);
}
/**
* 右边的文字
*
* @param text
*/
public void setTvRightText(String text) {
tvRight.setText(text);
}
}
使用
//嘎嘣脆
@LayoutId(R.layout.activity_main)
@TitleText("主页")
@LeftImag(R.drawable.back_defaut)
@RightText("你好啊")
public class MainActivity extends BaseActivity<BaseBean>{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void initView() {
}
@Override
public void initEvent() {
}
@Override
protected void handleMessageThis(Message msg, int what) {
}
@Override
protected void onDataEvent(BaseEventBean<BaseBean> event) {
}
}
效果图类似,就不贴了,我去写log框架...
两种类型的BaseActivity的实现方式各有千秋,看大佬你喜好了
代码见我的另一个博文Rxjava2的网络框架搭建。
抠脚来的不喜勿喷
下一篇: 定时器任务