Fast taps (clicks) on RecyclerView opens multiple Fragments

前端 未结 10 934
渐次进展
渐次进展 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: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
        }
    }
    

提交回复
热议问题