Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make RecyclerView's items non clickable?

I tried the following code:

view.setEnabled(false);
myReqRecyclerView.setEnabled(false);

The first line disables the current view which is clicked. After one click, I want to set all items non clickable so that double click should not happen. It seems that the second line is not working.

like image 530
Ritz Avatar asked Nov 27 '15 06:11

Ritz


2 Answers

First Simple option

but quite bad actually would by to use System time :

 long mLastClickTime = 0;

    youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                return;
            }
            mLastClickTime = SystemClock.elapsedRealtime();

            ....//your code


        }
    });

but for a clean programming I would recommend doing this :

A bit trickier but as simple

In your Adapter introduce a boolean variable, please pay attention to the viewHolder() and onBindViewHolder() methods :

private boolean clickable = true;


@Override
public void onBindViewHolder(@NonNull myAdapter.ViewHolder viewHolder, int position) {

    if (!clickable){
        viewHolder.cardView.setClickable(false);
    } else {
        viewHolder.cardView.setClickable(true);
    }

    //....
}

public void setClickable(boolean clickable) {
    this.clickable = clickable;
    myadapter.notifyDataSetChanged();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;

    public ViewHolder(View itemView, final myViewHolder.OnItemClickListener listener){
        super(itemView);
        //..

        cardView = itemView.findViewById(R.id.cardView);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(position);
                    }
                }
            }
        });
    }
}

and use myadapter.setClickable(state) as you wish

like this :

    youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            myadapter.setClickable(false);

            ....//your code


        }
    });

and maybe in your onResume() :

 myadapter.setClickable(true);
like image 95
Arnaud Avatar answered Sep 24 '22 08:09

Arnaud


try view.setClickable(false); on the view which you dont want to be clickable

like image 24
Sagar Pujari Avatar answered Sep 22 '22 08:09

Sagar Pujari