How to Disable Recyclerview Items from Clicking

How to disable RecyclerView Items from clicking

You can add a simple boolean to your adapter like this:

public boolean isClickable = true;

and set it in your fab-click:

mAdapter.isClickable = true/false;

And within your OnClickListener in the Adapter, only act when it is clickable:

public void onClick(View view) {
if(!isClickable)
return;
// do your click stuff
}

Disable click/touch for some of a RecyclerView's items

Probably the easiest way to completely block interaction with anything inside a single item is to put a transparent view over it that intercepts all touch events. You'd do this by wrapping your existing itemView layout in a FrameLayout and adding another view on top of that:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- your itemView content here -->

<View
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

Inside onCreateViewHolder(), you can assign a no-op click listener to the overlay:

@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.itemview, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);

holder.overlay.setOnClickListener(v -> {});

return holder;
}

Now, when you want to disable clicks, you can call

holder.overlay.setVisibility(View.VISIBLE);

and when you want to disable them, you can call

holder.overlay.setVisibility(View.GONE);

How to disable recycler view item selection

This will work:

Use boolean variable and setting default value to false and change value on button click.

boolean touch = false;

movieListRecyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// true: consume touch event
// false: dispatch touch event
return touch;
}
});

and on your button click or whatever click change boolean value;

 onClick(){
touch = !touch;
}

how can disable RecyclerView click listener but scroll working

after some research and thinking i do this :
pass all i need in parent RecyclerView to child RecyclerView adapter constructor with position like below :

   @Override
public void onBindViewHolder(final ChatList_Adapter.ViewHolder holder, final int position) {

holder.name.setText(chanelsList.get(i).getUser().getFirstname()+" "+chanelsList.get(i).getUser().getLastname());

holder.city_recycler_hosts.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
holder.city_recycler_hosts.setLayoutManager(layoutManager);
holder.city_recycler_hosts.addItemDecoration(new VerticalDividerItemDecoration.Builder(context).color(ContextCompat.getColor(context,R.color.lightergray)).margin(0,20).
build());

CityHosts_Adapter adapter=new CityHosts_Adapter(chanelsList.get(i).getUser().getCities(),chanelsList,file,position,pusher
,auth,message_ist);
holder.city_recycler_hosts.setAdapter(adapter);
}

then in child RecyclerView do what we done in parent RecyclerView click , but we pass position of parent RecyclerView to constructor adapter of child RecyclerView so Instead use getAdapterPosition in child we use position that we got in constructor

How to disable RecyclerView scrolling?

You should override the layoutManager of your recycleView for this. This way it will only disable scrolling, none of the other functionalities. You will still be able to handle click or any other touch events. For example:-

Original:

public class CustomGridLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;

public CustomGridLayoutManager(Context context) {
super(context);
}

public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}

@Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
return isScrollEnabled && super.canScrollVertically();
}
}

Here using "isScrollEnabled" flag you can enable/disable scrolling functionality of your recycle-view temporarily.

Also:

Simple override your existing implementation to disable scrolling and allow clicking.

linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};

In Kotlin:

object : LinearLayoutManager(this){ override fun canScrollVertically(): Boolean { return false } }

Disable RecyclerView Buttons

Declare a public boolean variable in your adapter and set it's default value to false;

public boolean clicked = false

inside onBindViewHolder check if (clicked) myHolder.btn.setEnabled(false); else myHolder.btn.setEnabled(true)

and in your click method set clicked = true and call notifyDataSetChanged()

this will disable all buttons,

if you want to disable all other buttons(except this one) use an integer variable insted of boolean public Integer clickedPosition = null

and check if (clickedPosition != null && clickedPosition != getAdapterPosition) myHolder.btn.setEnabled(false); else myHolder.btn.setEnabled(true);

and in your click method set clickedPosition = getAdapterPosition and call notifyDataSetChanged()

public  Integer clickedPosition;

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=inflater.inflate(R.layout.container_fish, viewGroup,false);
MyHolder holder=new MyHolder(view);
return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

final MyHolder myHolder= (MyHolder) viewHolder;
final DataFish current=data.get(i);

myHolder.textFishName.setText(current.fishName);
myHolder.textSize.setText("Size: " + current.sizeName);
myHolder.textType.setText("Category: " + current.catName);
if(clickedPosition!=null && clickedPosition!=getAdapterPosition()) myHolder.btn.setEnabled(false);
else myHolder.btn.setEnabled(false);
myHolder.btn.setOnClickListener( new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
clickedPosition = getAdapterPosition();
notifyDatasetChanged();
//code here

}
} );

myHolder.btn1.setOnClickListener( new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {

//code here

}
} );
myHolder.btn2.setOnClickListener( new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
clickedPosition = null;
notifyDatasetChanged();
//code here

}
} );


Related Topics



Leave a reply



Submit