Skip to content

Commit

Permalink
修复finalActivity无法绑定事件的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Oct 8, 2013
1 parent 1ca697a commit 2fd1812
Showing 1 changed file with 64 additions and 124 deletions.
188 changes: 64 additions & 124 deletions src/net/tsz/afinal/FinalActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,169 +21,109 @@
import net.tsz.afinal.annotation.view.Select;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;

public class FinalActivity extends Activity {
public abstract class FinalActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
FinalActivity.initInjectedView(this);
initView();
}


public void setContentView(View view, LayoutParams params) {
super.setContentView(view, params);
FinalActivity.initInjectedView(this);
initView();
}



public void setContentView(View view) {
super.setContentView(view);
FinalActivity.initInjectedView(this);
initView();
}

/**
* 初始化Actvity中的注入属性
* 可用于与其他框架合用(如ActionBarShelock)
* <p>
* *必须在setContentView之后调用:
* <pre>
* protected void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
* setContentView(view);
* FinalActivity.initInjectedView(this);
* }
* </pre>
* @param sourceActivity
*/
public static void initInjectedView(Activity sourceActivity){
initInjectedView(sourceActivity,sourceActivity.getWindow().getDecorView());
}

/**
* 初始化指定View中的注入属性
* 可用于Fragment内使用InjectView<p>
* 示例:<p>
* 在onCreateView中:
* <pre>
* public View onCreateView(LayoutInflater inflater, ViewGroup container,
* Bundle savedInstanceState) {
* View viewRoot = inflater.inflate(R.layout.map_frame, container, false);
* FinalActivity.initInjectedView(this,viewRoot);
* }
* </pre>
* @param sourceView
*/
public static void initInjectedView(Object injectedSource,View sourceView){
Field[] fields = injectedSource.getClass().getDeclaredFields();
private void initView(){
Field[] fields = getClass().getDeclaredFields();
if(fields!=null && fields.length>0){
for(Field field : fields){
ViewInject viewInject = field.getAnnotation(ViewInject.class);
if(viewInject!=null){
int viewId = viewInject.id();
try {
field.setAccessible(true);
/*当已经被赋值时,不在重复赋值,用于include,inflate情景下的viewinject组合*/
if(field.get(injectedSource)==null){
field.set(injectedSource,sourceView.findViewById(viewId));
}else{
continue;
}
} catch (Exception e) {
e.printStackTrace();
}

String clickMethod = viewInject.click();
if(!TextUtils.isEmpty(clickMethod))
setViewClickListener(injectedSource,field,clickMethod);
try {
field.setAccessible(true);

String longClickMethod = viewInject.longClick();
if(!TextUtils.isEmpty(longClickMethod))
setViewLongClickListener(injectedSource,field,longClickMethod);

String itemClickMethod = viewInject.itemClick();
if(!TextUtils.isEmpty(itemClickMethod))
setItemClickListener(injectedSource,field,itemClickMethod);


String itemLongClickMethod = viewInject.itemLongClick();
if(!TextUtils.isEmpty(itemLongClickMethod))
setItemLongClickListener(injectedSource,field,itemLongClickMethod);

Select select = viewInject.select();
if(!TextUtils.isEmpty(select.selected()))
setViewSelectListener(injectedSource,field,select.selected(),select.noSelected());
if(field.get(this)!= null )
continue;

ViewInject viewInject = field.getAnnotation(ViewInject.class);
if(viewInject!=null){

int viewId = viewInject.id();
field.set(this,findViewById(viewId));

setListener(field,viewInject.click(),Method.Click);
setListener(field,viewInject.longClick(),Method.LongClick);
setListener(field,viewInject.itemClick(),Method.ItemClick);
setListener(field,viewInject.itemLongClick(),Method.itemLongClick);

Select select = viewInject.select();
if(!TextUtils.isEmpty(select.selected())){
setViewSelectListener(field,select.selected(),select.noSelected());
}

}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


private static void setViewClickListener(Object injectedSource,Field field,String clickMethod){
try {
Object obj = field.get(injectedSource);
if(obj instanceof View){
((View)obj).setOnClickListener(new EventListener(injectedSource).click(clickMethod));
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static void setViewLongClickListener(Object injectedSource,Field field,String clickMethod){
try {
Object obj = field.get(injectedSource);
if(obj instanceof View){
((View)obj).setOnLongClickListener(new EventListener(injectedSource).longClick(clickMethod));
}
} catch (Exception e) {
e.printStackTrace();
private void setViewSelectListener(Field field,String select,String noSelect)throws Exception{
Object obj = field.get(this);
if(obj instanceof View){
((AbsListView)obj).setOnItemSelectedListener(new EventListener(this).select(select).noSelect(noSelect));
}
}

private static void setItemClickListener(Object injectedSource,Field field,String itemClickMethod){
try {
Object obj = field.get(injectedSource);
if(obj instanceof AbsListView){
((AbsListView)obj).setOnItemClickListener(new EventListener(injectedSource).itemClick(itemClickMethod));
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static void setItemLongClickListener(Object injectedSource,Field field,String itemClickMethod){
try {
Object obj = field.get(injectedSource);
if(obj instanceof AbsListView){
((AbsListView)obj).setOnItemLongClickListener(new EventListener(injectedSource).itemLongClick(itemClickMethod));
}
} catch (Exception e) {
e.printStackTrace();
private void setListener(Field field,String methodName,Method method)throws Exception{
if(methodName == null || methodName.trim().length() == 0)
return;

Object obj = field.get(this);

switch (method) {
case Click:
if(obj instanceof View){
((View)obj).setOnClickListener(new EventListener(this).click(methodName));
}
break;
case ItemClick:
if(obj instanceof AbsListView){
((AbsListView)obj).setOnItemClickListener(new EventListener(this).itemClick(methodName));
}
break;
case LongClick:
if(obj instanceof View){
((View)obj).setOnLongClickListener(new EventListener(this).longClick(methodName));
}
break;
case itemLongClick:
if(obj instanceof AbsListView){
((AbsListView)obj).setOnItemLongClickListener(new EventListener(this).itemLongClick(methodName));
}
break;
default:
break;
}
}

private static void setViewSelectListener(Object injectedSource,Field field,String select,String noSelect){
try {
Object obj = field.get(injectedSource);
if(obj instanceof View){
((AbsListView)obj).setOnItemSelectedListener(new EventListener(injectedSource).select(select).noSelect(noSelect));
}
} catch (Exception e) {
e.printStackTrace();
}
public enum Method{
Click,LongClick,ItemClick,itemLongClick
}


}

0 comments on commit 2fd1812

Please sign in to comment.