Reputation:
I have a RecyclerView in my app and it displays data. I want to disable user touch events of RecyclerView. I have tried the below code but it does not work.
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
Please help me.
Upvotes: 22
Views: 21669
Reputation: 677
You can use yourRecyclerView.suppressLayout(true)
to disable user interaction
Upvotes: 8
Reputation: 3215
Java:
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return true;
}
});
Kotlin:
recyclerView.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
return true
}
})
Just use this, fixed my problems (;
Upvotes: 13
Reputation: 2701
If you return true
touch events are disabled.
if Return false
touch events are enable.
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
return true;
}
});
hope it helps..
Upvotes: 8
Reputation: 99
you can use RecyclerView function
public void setLayoutFrozen(boolean frozen)
Upvotes: 9
Reputation: 263
As I had a similar use case a few days ago and stumbled upon this question just now:
you can add either
to your RecyclerView.
Example using RecyclerView.SimpleOnItemTouchListener
:
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// true: consume touch event
// false: dispatch touch event
return true;
}
});
I consumed the touch event while my SwipeRefreshLayout
was doing background work by querying swipeLayout.isRefreshing()
.
Upvotes: 24
Reputation: 117
You can override the LinearLayoutManager canScrollHorizontally
method
getRecyclerView().setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false) {
@Override
public boolean canScrollHorizontally() {
return false;
}
});
Upvotes: 0