可以不从FinalActivity继承吗?
前面有兄弟问了,如果必须从FinalActivity继承的话,可能会和其他应用冲突。
我把FinalActivity改了改,加了一个新类,在Activity的onCreate中调用就可以了,调用方法如下:
new FinalActivityHelper(this).initView();
FinalActivityHelper类的代码如下,这个类的代码都是从FinalActivity拷贝过来的,只改了几个小地方。
package net.tsz.afinal; import java.lang.reflect.Field; import net.tsz.afinal.annotation.view.EventListener; import net.tsz.afinal.annotation.view.Select; import net.tsz.afinal.annotation.view.ViewInject; import android.app.Activity; import android.text.TextUtils; import android.view.View; import android.widget.AbsListView; public class FinalActivityHelper { private Activity activity ; public FinalActivityHelper(Activity activity) { this.activity = activity; } public void initView() { Field[] fields = activity.getClass().getDeclaredFields(); if(fields == null || fields.length == 0) return ; for(Field field : fields) { ViewInject viewInject = field.getAnnotation(ViewInject.class); if(viewInject == null) continue ; int viewId = viewInject.id(); View view = activity.findViewById(viewId); if ( view == null ) continue ; try { field.setAccessible(true); field.set(activity,view ); } catch (Exception e) { e.printStackTrace(); } String clickMethod = viewInject.click(); if(!TextUtils.isEmpty(clickMethod)) setViewClickListener(view,clickMethod); String longClickMethod = viewInject.longClick(); if(!TextUtils.isEmpty(longClickMethod)) setViewLongClickListener(view,longClickMethod); String itemClickMethod = viewInject.itemClick(); if(!TextUtils.isEmpty(itemClickMethod)) setItemClickListener(view,itemClickMethod); String itemLongClickMethod = viewInject.itemLongClick(); if(!TextUtils.isEmpty(itemLongClickMethod)) setItemLongClickListener(view,itemLongClickMethod); Select select = viewInject.select(); if(!TextUtils.isEmpty(select.selected())) setViewSelectListener(view,select.selected(),select.noSelected()); } } private void setViewClickListener(View view,String clickMethod) { view.setOnClickListener(new EventListener(activity).click(clickMethod)); } private void setViewLongClickListener(View view,String clickMethod) { view.setOnLongClickListener(new EventListener(activity).longClick(clickMethod)); } private void setItemClickListener(View view,String itemClickMethod) { if(view instanceof AbsListView) { ((AbsListView)view).setOnItemClickListener(new EventListener(activity).itemClick(itemClickMethod)); } } private void setItemLongClickListener(View view,String itemClickMethod) { if(view instanceof AbsListView) { ((AbsListView)view).setOnItemLongClickListener(new EventListener(activity).itemLongClick(itemClickMethod)); } } private void setViewSelectListener(View view,String select,String noSelect) { if(view instanceof AbsListView) { ((AbsListView)view).setOnItemSelectedListener(new EventListener(activity).select(select).noSelect(noSelect)); } } }
另外,FinalActivity 第144行setViewSelectLister函数中
if(obj instanceof View){
是否应该改为
if(view instanceof AbsListView)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@afinal