From 34e238977311068536e0af219119836a85504f52 Mon Sep 17 00:00:00 2001 From: daimajia Date: Thu, 11 Sep 2014 19:36:15 +0800 Subject: [PATCH] fix #19 --- .../java/com/daimajia/swipe/SwipeLayout.java | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 46f33ea1..91ba8738 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -11,8 +11,12 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; +import android.widget.Adapter; import android.widget.AdapterView; +import android.widget.BaseAdapter; import android.widget.FrameLayout; +import android.widget.HeaderViewListAdapter; +import android.widget.ListAdapter; import java.util.ArrayList; import java.util.HashMap; @@ -713,8 +717,12 @@ private boolean childNeedHandleTouchEvent(View v, MotionEvent event){ @Override public boolean onTouchEvent(MotionEvent event) { + if(!isEnabledInAdapterView()) + return true; + int action = event.getActionMasked(); ViewParent parent = getParent(); + gestureDetector.onTouchEvent(event); Status status = getOpenStatus(); ViewGroup touching = null; @@ -818,6 +826,46 @@ public boolean onTouchEvent(MotionEvent event) { return true; } + /** + * if working in {@link android.widget.AdapterView}, we should response {@link android.widget.Adapter} + * isEnable(int position). + * @return true when item is enabled, else disabled. + */ + private boolean isEnabledInAdapterView(){ + AdapterView adapterView = getAdapterView(); + boolean enable = true; + if(adapterView != null){ + Adapter adapter = adapterView.getAdapter(); + if(adapter != null){ + int p = adapterView.getPositionForView(SwipeLayout.this); + if(adapter instanceof BaseAdapter){ + enable &= ((BaseAdapter) adapter).isEnabled(p); + }else if(adapter instanceof ListAdapter){ + enable &= ((ListAdapter) adapter).isEnabled(p); + }else if(adapter instanceof HeaderViewListAdapter){ + enable &= ((HeaderViewListAdapter) adapter).isEnabled(p); + } + } + } + return enable; + } + + private boolean insideAdapterView(){ + return getAdapterView() != null; + } + + private AdapterView getAdapterView(){ + ViewParent t = getParent(); + while(t != null){ + if(t instanceof AdapterView){ + return (AdapterView)t; + } + t = t.getParent(); + } + return null; + } + + private GestureDetector gestureDetector = new GestureDetector(getContext(), new SwipeDetector()); class SwipeDetector extends GestureDetector.SimpleOnGestureListener{ @@ -838,9 +886,10 @@ public boolean onSingleTapUp(MotionEvent e) { ViewParent t = getParent(); while(t != null) { if(t instanceof AdapterView){ - int p = ((AdapterView) t).getPositionForView(SwipeLayout.this); + AdapterView view = (AdapterView)t; + int p = view.getPositionForView(SwipeLayout.this); if( p != AdapterView.INVALID_POSITION && - ((AdapterView) t).performItemClick(((AdapterView) t).getChildAt(p),p , ((AdapterView) t).getAdapter().getItemId(p))) + view.performItemClick(view.getChildAt(p), p, view.getAdapter().getItemId(p))) return true; }else{ if(t instanceof View && ((View) t).performClick())