集成方法
支持aspectj和jitpack,在工程的grade添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ... dependencies { classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4' } ...
//Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
|
在使用的module的grade中添加依赖和注解处理器,以及支持aspectj
1 2 3 4 5 6
| ... apply plugin: 'android-aspectjx' ...
implementation 'com.github.tyhjh.Annotation:annotationlibrary:v1.1.8' annotationProcessor 'com.github.tyhjh.Annotation:annotator:v1.1.8'
|
需要支持lambda 表达式,在模块的build.gradle的android节点下面添加支持
1 2 3 4
| compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' }
|
使用方法
控件的依赖注入
初始化
需要使用注解的类中需要初始化
1 2 3 4 5
| protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CatAnnotation.injectView(this);
|
@ViewById
该注解用于控件的初始化,可以传入控件的ID,当控件的ID和变量名一致是可以不传入
1 2 3
| @ViewById TextView tvLogin,tvName;
|
被注解的变量名可以和控件ID值不一样,但是需要手动设置控件ID值,这种方法只能在app
模块使用
1 2 3
| @ViewById(R.id.et_pwd) EditText etPwd;
|
@Click
该注解为设置控件的点击事件,同样可以传入控件ID,改方法不需要初始化控件,可以直接使用
1 2 3 4 5 6 7 8 9
| @Click void tvLogin() { }
@Click void tvLogin(R.id.tvLogin) { }
|
点击防抖动
新增防抖动功能,使用了@Click
注解的按钮默认在300ms内只能点击一次,可以通过设置全局修改,也可以修改单个点击事件的间隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| AvoidShake.setClickIntervalTime(1000);
@Click(interval = 100) void txtView() { ... }
txtView.setOnClickListener(new AvoidShakeClickHelper(500, new AvoidShakeListener() { @Override public void onClick(View v) { } }));
|
@Background
该注解为方法切换到子线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay
值默认为0
1 2 3 4
| @Background(delay = 1000) void backgroud() { Log.e("backgroud", Thread.currentThread().getName() + ":" + System.currentTimeMillis()); }
|
@UiThread
该注解为方法切换到主线程运行,可以进行延迟执行,方法不能有返回值,否则不生效,delay
值默认为0
1 2 3 4
| @UiThread(delay = 1000 * 5) void toast(String msg) { Log.e("UiThread", Thread.currentThread().getName() + ":" + System.currentTimeMillis()); }
|
@RecyclerMore
该注解为RecyclerView滑动到底部监听,可以实现下拉加载更多的功能,使用很方便,变量名就是方法名,也可以手动设置控件ID值;可以设置pageSize
的值,如果当前加载的item数量小于pageSize
那么就不会触发方法,默认滑动到底部监听就会触发方法
1 2 3 4 5
| @RecyclerMore(pageSize = 5) void ryclView() { mList.addAll(mList2); mAdapter.notifyDataSetChanged(); }
|
@CheckBoxChange
该注解为CheckBox的OnCheckedChange监听方法,被注解的方法需要有下面两个参数
1 2 3 4
| @CheckBoxChange void swTest(boolean isChecked, CompoundButton swTest) { Toast.makeText(this, "isChecked:" + swTest.isChecked(), Toast.LENGTH_SHORT).show(); }
|
@ExecuteTime
该注解为获取被注解的方法执行的时间
1 2 3 4 5 6 7 8 9 10 11
| @ExecuteTime private void etTest() { Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show(); }
ExecuteManager.getInstance().setPrinter((executeTime, annotion, methodInfo) -> { Log.i(TAG,"方法耗时为: "+executeTime); Log.i(TAG,"方法的详情:"+methodInfo.toString()); });
|
@CustomAnnotation
该注解为自定义注解,用于监听方法执行使用,可以用于数据埋点啥的;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @CustomAnnotation private void etTest() { Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show(); }
ExecuteManager.getInstance().addExecuteListener(new IExecuteListener() { @Override public void before(CustomAnnotation annotation, MethodInfo methodInfo) { Log.i(TAG,"before MethodInfo is "+methodInfo.toString()); }
@Override public void after(CustomAnnotation annotation, MethodInfo methodInfo) { Log.i(TAG,"after MethodInfo is "+methodInfo.toString()); } });
|
相关推荐文章
项目地址
查看最新版本:https://github.com/tyhjh/Annotation