daimajia / AndroidSwipeLayout

The Most Powerful Swipe Layout!
MIT License
12.38k stars 2.67k forks source link

swipeLayout.addSwipeListener methods called multiple times #219

Open karthik844 opened 9 years ago

karthik844 commented 9 years ago

When i swipe, the onOpen methods is called multiple times. It varies every time. Sometimes, it's called once, sometimes twice, most times thrice or more. Could you please help?

    public class ItemListAdapter extends ArrayAdapter<ItemDetails> {
    Context cContext;

    / *        //return the SwipeLayout resource id in the layout.
    public int getSwipeLayoutResourceId(int position) {

    }*/

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onClose(SwipeLayout layout) {
                //when the SurfaceView totally cover the BottomView.
     //                    Log.i("swipelayout", "onClose");
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
                //you are swiping.
                Log.i("swipelayout", "onUpdate " + " " + leftOffset + " " + topOffset);
            }

            @Override
            public void onStartOpen(SwipeLayout layout) {
    //                    Log.i("swipelayout", "onStartOpen");
            }

            @Override
            public void onOpen(final SwipeLayout layout) {

                    if (layout.getOpenStatus() == SwipeLayout.Status.Open && layout.isSwipeEnabled()) {
                        layout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                layout.close();
                                if (layout.getDragEdge() == SwipeLayout.DragEdge.Left) {
                                    Log.i("swipelayout", "DELETE Item");
                                } else if (layout.getDragEdge() == SwipeLayout.DragEdge.Right) {
                                    Log.i("swipelayout", "Item PAID");
                                    paidItem(ItemObject);
                                }
                            }
                        }, 500);
                    }
            }

            @Override
            public void onStartClose(SwipeLayout layout) {
                Log.i("swipelayout", "onStartClose");
            }

            @Override
            public void onHandRelease(final SwipeLayout layout, final float xvel, final float yvel) {
                //when user's hand released.
                Log.i("swipelayout", "onHandRelease " + " " + xvel + " " + yvel);
            }
        });

    }

public class ViewHolder {
...
}

}

HugoGresse commented 9 years ago

I have the same issue, it seems it's called multiple times until we release the finger ad the view is open. Here is a fix I've used :

        private boolean mIsOpen;

        @Override
        public void onStartOpen(SwipeLayout swipeLayout) {
        }

        @Override
        public void onOpen(SwipeLayout swipeLayout) {
            mIsOpen = true;
        }

        @Override
        public void onStartClose(SwipeLayout swipeLayout) {
        }

        @Override
        public void onClose(SwipeLayout swipeLayout) {
            mIsOpen = false;
        }

        @Override
        public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {
        }

        @Override
        public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {
            if (mIsOpen) {
                // To your onOpen stuff
            }
        }
karthik844 commented 9 years ago

Thanks HugoGresse. I will try that.

tsakmalis commented 7 years ago

Hello,

Have you found any solution to this problem?

carmas123 commented 7 years ago

@tsakmalis I've a solution for you... I've created a simple derived SwipeLayout class that you can use for handle listener events direct from SwipeLayout:

import android.content.Context
import android.util.AttributeSet
import com.daimajia.swipe.SwipeLayout

open class SwipeLayoutEx : com.daimajia.swipe.SwipeLayout {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)

    private val mSwipeListener = SwipeListenerBase()

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        this.addSwipeListener(mSwipeListener)
    }

    override fun onDetachedFromWindow() {
        this.removeSwipeListener(mSwipeListener)
        super.onDetachedFromWindow()
    }

    inner class SwipeListenerBase : SwipeLayout.SwipeListener {
        override fun onOpen(layout: SwipeLayout) {
            mOnSwipeOpen?.invoke(layout)
        }

        override fun onUpdate(layout: SwipeLayout, leftOffset: Int, topOffset: Int) {
            mOnSwipeUpdate?.invoke(layout, leftOffset, topOffset)
        }

        override fun onStartOpen(layout: SwipeLayout?) {
            mOnSwipeStartOpen?.invoke(layout)
        }

        override fun onStartClose(layout: SwipeLayout?) {
            mOnSwipeStartClose?.invoke(layout)
        }

        override fun onHandRelease(layout: SwipeLayout?, xvel: Float, yvel: Float) {
            mOnSwipeHandRelease?.invoke(layout, xvel, yvel)
        }

        override fun onClose(layout: SwipeLayout?) {
            mOnSwipeClose?.invoke(layout)
        }
    }

    private var mOnSwipeOpen: ((layout: SwipeLayout?) -> Unit)? = null
    private var mOnSwipeClose: ((layout: SwipeLayout?) -> Unit)? = null
    private var mOnSwipeStartOpen: ((layout: SwipeLayout?) -> Unit)? = null
    private var mOnSwipeStartClose: ((layout: SwipeLayout?) -> Unit)? = null
    private var mOnSwipeHandRelease: ((layout: SwipeLayout?, xvel: Float, yvel: Float) -> Unit)? = null
    private var mOnSwipeUpdate: ((layout: SwipeLayout, leftOffset: Int, topOffset: Int) -> Unit)? = null

    fun onSwipeOpen(f: (layout: SwipeLayout?) -> Unit): SwipeLayoutEx {
        mOnSwipeOpen = f
        return this
    }

    fun onSwipeClose(f: (layout: SwipeLayout?) -> Unit): SwipeLayoutEx {
        mOnSwipeClose = f
        return this
    }

    fun onSwipeStartOpen(f: (layout: SwipeLayout?) -> Unit): SwipeLayoutEx {
        mOnSwipeStartOpen = f
        return this
    }

    fun onSwipeStartClose(f: (layout: SwipeLayout?) -> Unit): SwipeLayoutEx {
        mOnSwipeStartClose = f
        return this
    }

    fun onSwipeHandRelease(f: (layout: SwipeLayout?, xvel: Float, yvel: Float) -> Unit): SwipeLayoutEx {
        mOnSwipeHandRelease = f
        return this
    }

    fun onSwipeUpdate(f: (layout: SwipeLayout, leftOffset: Int, topOffset: Int) -> Unit): SwipeLayoutEx {
        mOnSwipeUpdate = f
        return this
    }
}

Now, you can use it like that:

mySwipeLayout
  .onSwipeOpen { layout ->
    toast("You layout is opened!!")
  }.onSwipeClose {
    toast("You layout is closed!!")
  }
Maheshsmartdata commented 6 years ago

carmas123, can you please write it in java