public abstract class CustClickListener implements View.OnClickListener{
@Override
public void onClick(View view) {
if(!interceptViewClick(view)){
onViewClick(view);
}
}
protected boolean interceptViewClick(View view){
//TODO:这里可做一此通用的处理如打点,或拦截等。
return false;
}
protected abstract void onViewClick(View view);
}
CustClickListener mClickListener = new CustClickListener() {
@Override
protected void onViewClick(View view) {
Toast.makeText(CustActvity.this, view.toString(), Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.button).setOnClickListener(mClickListener);
}
public interface IProxyClickListener {
boolean onProxyClick(WrapClickListener wrap, View v);
class WrapClickListener implements View.OnClickListener {
IProxyClickListener mProxyListener;
View.OnClickListener mBaseListener;
public WrapClickListener(View.OnClickListener l, IProxyClickListener proxyListener) {
mBaseListener = l;
mProxyListener = proxyListener;
}
@Override
public void onClick(View v) {
boolean handled = mProxyListener == null ? false : mProxyListener.onProxyClick(WrapClickListener.this, v);
if (!handled && mBaseListener != null) {
mBaseListener.onClick(v);
}
}
}
}
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
hookViews(rootView, 0)
}
});
public void init() {
if (sHookMethod == null) {
try {
Class viewClass = Class.forName("android.view.View");
if (viewClass != null) {
sHookMethod = viewClass.getDeclaredMethod("getListenerInfo");
if (sHookMethod != null) {
sHookMethod.setAccessible(true);
}
}
} catch (Exception e) {
reportError(e, "init");
}
}
if (sHookField == null) {
try {
Class listenerInfoClass = Class.forName("android.view.View$ListenerInfo");
if (listenerInfoClass != null) {
sHookField = listenerInfoClass.getDeclaredField("mOnClickListener");
if (sHookField != null) {
sHookField.setAccessible(true);
}
}
} catch (Exception e) {
reportError(e, "init");
}
}
}
private void hookViews(View view, int recycledContainerDeep) {
if (view.getVisibility() == View.VISIBLE) {
boolean forceHook = recycledContainerDeep == 1;
if (view instanceof ViewGroup) {
boolean existAncestorRecycle = recycledContainerDeep > 0;
ViewGroup p = (ViewGroup) view;
if (!(p instanceof AbsListView || p instanceof RecyclerView) || existAncestorRecycle) {
hookClickListener(view, recycledContainerDeep, forceHook);
if (existAncestorRecycle) {
recycledContainerDeep++;
}
} else {
recycledContainerDeep = 1;
}
int childCount = p.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = p.getChildAt(i);
hookViews(child, recycledContainerDeep);
}
} else {
hookClickListener(view, recycledContainerDeep, forceHook);
}
}
}
private void hookClickListener(View view, int recycledContainerDeep, boolean forceHook) {
boolean needHook = forceHook;
if (!needHook) {
needHook = view.isClickable();
if (needHook && recycledContainerDeep == 0) {
needHook = view.getTag(mPrivateTagKey) == null;
}
}
if (needHook) {
try {
Object getListenerInfo = sHookMethod.invoke(view);
View.OnClickListener baseClickListener = getListenerInfo == null ? null : (View.OnClickListener) sHookField.get(getListenerInfo);//获取已设置过的监听器
if ((baseClickListener != null && !(baseClickListener instanceof IProxyClickListener.WrapClickListener))) {
sHookField.set(getListenerInfo, new IProxyClickListener.WrapClickListener(baseClickListener, mInnerClickProxy));
view.setTag(mPrivateTagKey, recycledContainerDeep);
}
} catch (Exception e) {
reportError(e,"hook");
}
}
}
public boolean performClick() {
final boolean result;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(this);
result = true;
} else {
result = false;
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
return result;
}
public void sendAccessibilityEvent(int eventType) {
if (mAccessibilityDelegate != null) {
mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
} else {
sendAccessibilityEventInternal(eventType);
}
}
public void setAccessibilityDelegate(@Nullable AccessibilityDelegate delegate) {
mAccessibilityDelegate = delegate;
}
public class ViewClickTracker extends View.AccessibilityDelegate {
boolean mInstalled = false;
WeakReference<View> mRootView = null;
ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = null;
public ViewClickTracker(View rootView) {
if (rootView != null && rootView.getViewTreeObserver() != null) {
mRootView = new WeakReference(rootView);
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View root = mRootView == null ? null : mRootView.get();
boolean install = ;
if (root != null && root.getViewTreeObserver() != null && root.getViewTreeObserver().isAlive()) {
try {
installAccessibilityDelegate(root);
if (!mInstalled) {
mInstalled = true;
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
destroyInner(false);
}
}
};
rootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
}
}
private void installAccessibilityDelegate(View view) {
if (view != null) {
view.setAccessibilityDelegate(ViewClickTracker.this);
if (view instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) view;
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
View child = parent.getChildAt(i);
if (child.getVisibility() != View.GONE) {
installAccessibilityDelegate(child);
}
}
}
}
}
@Override
public void sendAccessibilityEvent(View host, int eventType) {
super.sendAccessibilityEvent(host, eventType);
if (AccessibilityEvent.TYPE_VIEW_CLICKED == eventType && host != null) {
//TODO 这里处理通用的点击事件,host 即为相应被点击的 View.
}
}
}
// First touch target in the linked list of touch targets.
private TouchTarget mFirstTouchTarget;
public boolean dispatchTouchEvent(MotionEvent ev) {
......
if (newTouchTarget == null && childrenCount != 0) {
for (int i = childrenCount - 1; i >= 0; i--) {
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
}
}
......
// Dispatch to touch targets.
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS);
} else {
// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it. Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted;
......
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
}
}
//whether it could be a click action
public boolean isClickPossible(float slop) {
if (mCancel || mDownId == -1 || mUpId == -1 || mDownTime == 0 || mUpTime == 0) {
return false;
} else {
return Math.abs(mDownX - mUpX) < slop && Math.abs(mDownY - mUpY) < slop;
}
}
private boolean ensureTargetField() {
if (sTouchTargetField == null) {
try {
Class viewClass = Class.forName("android.view.ViewGroup");
if (viewClass != null) {
sTouchTargetField = viewClass.getDeclaredField("mFirstTouchTarget");
sTouchTargetField.setAccessible(true);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (sTouchTargetField != null) {
sTouchTargetChildField = sTouchTargetField.getType().getDeclaredField("child");
sTouchTargetChildField.setAccessible(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return sTouchTargetField != null && sTouchTargetChildField != null;
}
// find the target view who is interest in the touch event. null if not find
private View findTargetView() {
View nextTarget, target = null;
if (ensureTargetField() && mRootView != null) {
nextTarget = findTargetView(mRootView);
do {
target = nextTarget;
nextTarget = null;
if (target instanceof ViewGroup) {
nextTarget = findTargetView((ViewGroup) target);
}
} while (nextTarget != null);
}
return target;
}
//reflect to find the TouchTarget child view,null if not found .
private View findTargetView(ViewGroup parent) {
try {
Object target = sTouchTargetField.get(parent);
if (target != null) {
Object view = sTouchTargetChildField.get(target);
if (view instanceof View) {
return (View) view;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有