question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

swipeLayout.addSwipeListener methods called multiple times

See original GitHub issue

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 {
...
}

}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
HugoGressecommented, Sep 4, 2015

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
            }
        }
1reaction
carmas123commented, Jul 4, 2017

@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!!")
  }

Read more comments on GitHub >

github_iconTop Results From Across the Web

daimajia AndroidSwipeLayout Button click issue in Activity
When I set default fragment in onClose method of swipeLayout's addSwipeListener, it does not listen to button click.
Read more >
com.daimajia.swipe.SwipeLayout.addSwipeListener java ...
Popular methods of SwipeLayout · addDrag · findViewById · addRevealListener. bind multiple views with an com.daimajia.swipe.SwipeLayout. · close. close surface.
Read more >
Swipeable ListView in Android - Apna Java
Open build.gradle > add this line compile "com.daimajia.swipelayout:library:1.2.0@aar". > Sync the gradle file . Like this. dependencies {.
Read more >
Making Swipeable ListView in Android
This design is helpful in quickly actions with each ListView element (edit, delete, archive,...). By searching on Internet, we found that there ...
Read more >
SwipeLayout.java example - Javatips.net
This class describes the usage of SwipeLayout.java. ... float xvel, float yvel); } public void addSwipeListener(SwipeListener l) { mSwipeListeners.add(l); } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found