disable touch events of RecyclerView (to prevent u

2020-04-03 01:36发布

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.

5条回答
老娘就宠你
2楼-- · 2020-04-03 01:51

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

查看更多
不美不萌又怎样
3楼-- · 2020-04-03 01:52

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 (;

查看更多
爷、活的狠高调
4楼-- · 2020-04-03 02:10

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().

查看更多
看我几分像从前
5楼-- · 2020-04-03 02:13

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..

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-04-03 02:15

You can override the LinearLayoutManager canScrollHorizontally method

getRecyclerView().setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false) {
    @Override
    public boolean canScrollHorizontally() {
        return false;
    }
});
查看更多
登录 后发表回答