Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化,不再依赖反射获取OnClickListener #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,52 +391,18 @@ public void onClick(View view) {
}
}

public View.OnClickListener getOnClickListener(View view) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return getOnClickListenerV14(view);
} else {
return getOnClickListenerV(view);
}
}

private View.OnClickListener getOnClickListenerV(View view) {
View.OnClickListener retrievedListener = null;
try {
Field field = Class.forName(CLASS_NAME_VIEW).getDeclaredField("mOnClickListener");
field.setAccessible(true);
retrievedListener = (View.OnClickListener) field.get(view);
} catch (Exception e) {
e.printStackTrace();
}
private View.OnClickListener mOnClickLis;

return retrievedListener;
public View.OnClickListener getOnClickListener(View view) {
return mOnClickLis;
}

private View.OnClickListener getOnClickListenerV14(View view) {
View.OnClickListener retrievedListener = null;
try {
Field listenerField = Class.forName(CLASS_NAME_VIEW).getDeclaredField("mListenerInfo");
Object listenerInfo = null;

if (listenerField != null) {
listenerField.setAccessible(true);
listenerInfo = listenerField.get(view);
}

Field clickListenerField = Class.forName(CLASS_NAME_LISTENER_INFO).getDeclaredField("mOnClickListener");

if (clickListenerField != null && listenerInfo != null) {
clickListenerField.setAccessible(true);
retrievedListener = (View.OnClickListener) clickListenerField.get(listenerInfo);
}
} catch (Exception e) {
e.printStackTrace();
}

return retrievedListener;
@Override
public void setOnClickListener(View.OnClickListener l) {
super.setOnClickListener(l);
mOnClickLis = l;
}


/**
* Copy from:
* http://stackoverflow.com/questions
Expand Down