Fast taps (clicks) on RecyclerView opens multiple Fragments

前端 未结 10 935
渐次进展
渐次进展 2021-02-02 13:37

I have implemented onClick listener to my ViewHolder for my RecyclerView

But when I perform very fast double taps or mouse clicks, it performs the task (opens a seperate

10条回答
  •  抹茶落季
    2021-02-02 14:13

    You can make class implementing View.OnClickListener

    public class DoubleClickHelper implements View.OnClickListener {
    
        private long mLastClickTime = System.currentTimeMillis();
        private static final long CLICK_TIME_INTERVAL = 300;
        private Callback callback;
    
        public DoubleClickHelper(Callback callback) {
            this.callback = callback;
        }
    
        @Override
        public void onClick(View v) {
            long now = System.currentTimeMillis();
            if (now - mLastClickTime < CLICK_TIME_INTERVAL) {
                return;
            }
            mLastClickTime = now;
            callback.handleClick();
        }
    
        public interface Callback {
            void handleClick();
        }
    }
    

    And than use it like:

    ivProduct.setOnClickListener(new DoubleClickHelper(() -> listener.onProductInfoClick(wItem)));
    

提交回复
热议问题