Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast taps (clicks) on RecyclerView opens multiple Fragments

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 fragment in this case) twice or three times.

here is my code

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView tvTitle, tvDescription;

    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) {
        mListener.onClick(FRAGMENT_VIEW, getAdapterPosition()); // open FRAGMENT_VIEW
    }
}

Any ideas on how to prevent such behaviour?

like image 242
Shifatul Avatar asked Aug 07 '15 02:08

Shifatul


People also ask

How do I stop multiple clicks on Android?

Here's a OnClickListener proxy that prevents successive clicks, based on qezt's answer. button. setOnClickListener(new MultiClickGuard(new View. OnClickListener() { @Override public void onClick(View v) { // do something } }, 1000)); button.

How many times is called onCreateViewHolder?

By default it have 5. you can increase as per your need. Save this answer.

Why is RecyclerView used select one?

Recycler View you could say is an efficient way to create list of views. If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.

What is onCreateViewHolder?

onCreateViewHolder is called when you need a new View. If there is an available Recycled View that can be provided and be bound with new data, then onBindViewHolder is called :) 96. 96. 96.


1 Answers

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
    }
}
like image 61
Money Avatar answered Oct 21 '22 05:10

Money