Disable RecyclerView touch events (so as not to touch the user)?

I have a RecyclerView in my application and it displays data. I want to disable RecyclerView user touch events. I tried the code below, but it does not work.

recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); 

Please help me.

+6
source share
4 answers

Since I had a similar use case a few days ago, and I came across this question just now:
You can add either

to your RecyclerView.

RecyclerView.SimpleOnItemTouchListener example:

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

I used a touch event while my SwipeRefreshLayout doing background work requesting swipeLayout.isRefreshing() .

+11
source

you can use the RecyclerView function public void setLayoutFrozen(boolean frozen)

+5
source

You can override the LinearLayoutManager canScrollHorizontally method

 getRecyclerView().setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false) { @Override public boolean canScrollHorizontally() { return false; } }); 
0
source
 recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return true; } }); 

Just use this, fixed all my problems (;

0
source

Source: https://habr.com/ru/post/1247771/


All Articles