Skip to content

Commit

Permalink
hidden view touch dispatching fixed (hidden view wasn't touchable)
Browse files Browse the repository at this point in the history
  • Loading branch information
Knickedi committed Nov 21, 2011
1 parent d4192da commit 7289025
Showing 1 changed file with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,16 @@ public class SwipeableHiddenView extends FrameLayout implements SwipeableListIte
private boolean mLongClicked = false;
private boolean mStartedTwice = false;

private View [] mOverlayChild = new View [] {null};
private View [] mHiddenChild = new View [] {null};
private View [] mCurrentChildren = new View [] {null, null};

private static Field mChildren;
private static Field mChildrenCount;

static {
try {
mChildren = ViewGroup.class.getField("mChildren");
mChildren = ViewGroup.class.getDeclaredField("mChildren");
mChildren.setAccessible(true);
mChildrenCount = ViewGroup.class.getField("mChildrenCount");
mChildrenCount = ViewGroup.class.getDeclaredField("mChildrenCount");
mChildrenCount.setAccessible(true);
} catch (Exception e) {
mChildren = null;
Expand Down Expand Up @@ -902,19 +901,43 @@ public boolean dispatchTouchEvent(MotionEvent ev) {

try {
// FIXME we need a better way than reflection
// a rewrite of this method leads to bugs or crashes, so I decided for reflection
final Object oldChildren = mChildren.get(this);
final Object oldChildrenCount = mChildrenCount.get(this);

mChildren.set(this, isHiddenViewCovered() ? mOverlayChild : mHiddenChild);
mChildrenCount.set(this, 1);

boolean result = super.dispatchTouchEvent(ev);

mChildren.set(this, oldChildren);
mChildrenCount.set(this, oldChildrenCount);

return result;
// a rewrite of this method leads to bugs or crashes, so I decided for reflection
// basically we want to avoid that dispatching on down action is requesting touch
// on the wrong view, so we hide the children by reflection and restor afterwards
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
mCurrentChildren[0] = isHiddenViewCovered() ? mOverlayView : mHiddenView;
mChildren.set(this, mCurrentChildren);
mChildrenCount.set(this, 1);

boolean result = super.dispatchTouchEvent(ev);

if (mHiddenView != null) {
mCurrentChildren[0] = mHiddenView;

if (mOverlayView != null) {
mCurrentChildren[1] = mOverlayView;
mChildrenCount.set(this, 2);
} else {
mCurrentChildren[1] = null;
mChildrenCount.set(this, 1);
}
} else if (mOverlayView != null) {
mCurrentChildren[0] = mOverlayView;
mCurrentChildren[1] = null;
mChildrenCount.set(this, 1);
} else {
mCurrentChildren[0] = null;
mCurrentChildren[1] = null;
mChildrenCount.set(this, 0);
}

mChildren.set(this, mCurrentChildren);

return result;
} else {
return super.dispatchTouchEvent(ev);
}
} catch (Exception e) {
return super.dispatchTouchEvent(ev);
}
Expand Down Expand Up @@ -1014,7 +1037,6 @@ public void addView(View child, ViewGroup.LayoutParams params) {
public void addView(View child, int index, ViewGroup.LayoutParams params) {
checkAddView();
mOverlayView = child;
mOverlayChild[0] = child;
super.addView(child, -1, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
Expand All @@ -1035,12 +1057,10 @@ public void removeView(View view) {
mHiddenView.setDrawingCacheEnabled(false);
mHiddenViewCache.getLayoutParams().height = mHiddenView.getHeight();
mHiddenView = null;
mHiddenChild[0] = null;
}

if (mOverlayView == view) {
mOverlayView = null;
mOverlayChild[0] = null;
}

super.removeView(view);
Expand All @@ -1054,7 +1074,6 @@ public void removeView(View view) {
public void removeViewAt(int index) {
if (index != 0 && (mHiddenView == null || index > 1)) {
mOverlayView = null;
mOverlayChild[0] = null;
super.removeViewAt(index);
}
}
Expand All @@ -1066,9 +1085,9 @@ public void removeViewAt(int index) {
public void removeAllViews() {
int count = getChildCount();

if (mHiddenView != null && count > 2 || mHiddenView == null && count > 1) {
if (mHiddenView != null && count > 1 || mHiddenView == null && count > 0) {
mOverlayView = null;
mOverlayChild[0] = null;

super.removeViewAt(count - 1);
}
}
Expand Down Expand Up @@ -1238,8 +1257,6 @@ private void bindHiddenView() {
throw new NullPointerException("getHiddenView() did return null");
}

mHiddenChild[0] = mHiddenView;

if (mHiddenView.getParent() != null) {
((ViewGroup) mHiddenView.getParent()).removeView(mHiddenView);
}
Expand Down

0 comments on commit 7289025

Please sign in to comment.