From c0c05f08ce063f796e68a0944193fd07bb6cdbc4 Mon Sep 17 00:00:00 2001 From: Sven Braune Date: Wed, 21 Jun 2017 11:30:52 +0200 Subject: [PATCH] # Restore swipe position on Layout --- .../example/MyItemRecyclerViewAdapter.java | 1 + .../com/swipelibrary/DraggingProxy.java | 10 ++++ .../com/swipelibrary/LayoutCache.java | 52 +++++++++++++++++++ .../com/swipelibrary/SwipeLayout.java | 16 +++++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 swipelibrary/src/main/java/kaufland/com/swipelibrary/LayoutCache.java diff --git a/app/src/main/java/kaufland/com/demo/example/MyItemRecyclerViewAdapter.java b/app/src/main/java/kaufland/com/demo/example/MyItemRecyclerViewAdapter.java index da00d07..4b3b6a8 100644 --- a/app/src/main/java/kaufland/com/demo/example/MyItemRecyclerViewAdapter.java +++ b/app/src/main/java/kaufland/com/demo/example/MyItemRecyclerViewAdapter.java @@ -1,5 +1,6 @@ package kaufland.com.demo.example; +import android.inputmethodservice.Keyboard; import android.os.Looper; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java index 8acf469..529c5ad 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java @@ -24,6 +24,9 @@ public class DraggingProxy { @Bean protected SwipeViewLayouter mSwipeViewLayouter; + @Bean + protected LayoutCache mLayoutCache; + private boolean isInitilized; @@ -136,4 +139,11 @@ public int getSurfaceOpenOffsetByDragView(int dragView) { return mSwipeViewLayouter.getViewEngines().get(dragView).getOpenOffset(); } + public void captureChildrenBound() { + mLayoutCache.captureChildrenBound(mSwipeViewLayouter.getViews().values()); + } + + public void restoreChildrenBound() { + mLayoutCache.restoreOnLayout(); + } } diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/LayoutCache.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/LayoutCache.java new file mode 100644 index 0000000..0fc0bf7 --- /dev/null +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/LayoutCache.java @@ -0,0 +1,52 @@ +package kaufland.com.swipelibrary; + +import android.graphics.PointF; +import android.view.View; + +import org.androidannotations.annotations.EBean; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by sbra0902 on 21.06.17. + */ +@EBean +public class LayoutCache { + + + Map mLayoutCache = new HashMap<>(); + + + public void restoreOnLayout(){ + + for (View child : mLayoutCache.keySet()){ + + PointF restorePoint = mLayoutCache.get(child); + + if(restorePoint != null){ + child.setX(restorePoint.x); + child.setY(restorePoint.y); + } + + } + + } + + + + public void captureChildrenBound(Collection views){ + + for (View child : views) { + PointF point = mLayoutCache.get(child); + if(point==null){ + point = new PointF(); + mLayoutCache.put(child, point); + } + + point.x = child.getX(); + point.y = child.getY(); + } + } +} diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java index 90bcdfa..cee80c5 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java @@ -48,6 +48,9 @@ public class SwipeLayout extends FrameLayout { @Bean protected DraggingProxy mDraggingProxy; + @Bean + protected LayoutCache mLayoutCache; + private KDragViewHelper mDragHelper; private SwipeListener mSwipeListener; @@ -56,7 +59,6 @@ public class SwipeLayout extends FrameLayout { private float mDragHelperTouchSlop; - public interface SwipeListener { void onSwipeOpened(SwipeState.DragViewState openedDragView, boolean isFullSwipe); @@ -247,6 +249,12 @@ public void run() { } + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + mDraggingProxy.restoreChildrenBound(); + } + private class SwipeDragViewHelper extends ViewDragHelper.Callback { private ViewGroup parent; @@ -264,6 +272,12 @@ public boolean tryCaptureView(View child, int pointerId) { return mDraggingProxy.isCapturedViewDraggable(child); } + @Override + public void onViewDragStateChanged(int state) { + if(state == 0){ + mDraggingProxy.captureChildrenBound(); + } + } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {