Fast taps (clicks) on RecyclerView opens multiple Fragments

前端 未结 10 894
渐次进展
渐次进展 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:08
    • Create a boolean variable in Adapter
    boolean canStart = true;
    
    • Make OnClickListener like
    ViewHolder.dataText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (canStart) {
                canStart = false; // do canStart false 
                // Whatever you want to do and not have run twice due to double tap
            }
        }
    }
    
    • Add setCanStart method in Adapter class:
    public void setCanStart(boolean can){
        canStart = can;
    }
    
    • At last in the fragment's or Activity's ( Where the adapter is assigned to the recyclerview ) add this onResume()
    @Override
        public void onResume() {
            super.onResume();
            mAdapter.setCanStart(true);
        }
    

    Hope it will help :)

    0 讨论(0)
  • 2021-02-02 14:09

    If you are using Kotlin you can go with this based on Money's answer

    class CodeThrottle {
        companion object {
            const val MIN_INTERVAL = 300
        }
        private var lastEventTime = System.currentTimeMillis()
    
        fun throttle(code: () -> Unit) {
            val eventTime = System.currentTimeMillis()
            if (eventTime - lastEventTime > MIN_INTERVAL) {
                lastEventTime = eventTime
                code()
            }
        }
    }
    

    Create this object in your view holder

        private val codeThrottle = CodeThrottle()
    

    Then do the following in your bind

    name.setOnClickListener { codeThrottle.throttle { listener.onCustomerClicked(customer, false) } }
    

    Putting whatever code you need called in place of

    listener.onCustomerClicked(customer, false) 
    
    0 讨论(0)
  • 2021-02-02 14:12

    You can modify it like this.

    public class ViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener {
        TextView tvTitle, tvDescription;
        private long mLastClickTime = System.currentTimeMillis();
        private static final long CLICK_TIME_INTERVAL = 300;
    
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
    
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDescription = (TextView) itemView
                    .findViewById(R.id.tv_description);
        }
    
        @Override
        public void onClick(View v) {
            long now = System.currentTimeMillis();
            if (now - mLastClickTime < CLICK_TIME_INTERVAL) {
                return;
            }
            mLastClickTime = now;
            mListener.onClick(FRAGMENT_VIEW, getAdapterPosition()); // open
                                                                    // FRAGMENT_VIEW
        }
    }
    
    0 讨论(0)
  • 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)));
    
    0 讨论(0)
提交回复
热议问题