Open karthik844 opened 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
}
}
Thanks HugoGresse. I will try that.
Hello,
Have you found any solution to this problem?
@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!!")
}
carmas123, can you please write it in java
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?
}