codeMan
codeMan

Reputation: 75

Restrict user scrolling in RecyclerView

In my project I use a RecyclerView that I only want to scroll by calling the startSmoothScroll() method of the LayoutManager:

private fun next(){
    val layoutManager = pager.layoutManager as BattlePageLayoutManager
    layoutManager.startSmoothScroll(smoothScroller(layoutManager.findFirstVisibleItemPosition() + 1))
    layoutManager.finishScroll()
}

I do not want the user to be able to scroll manually, e. g. by swiping.

I already tried to achieve this through overriding the method onInterceptTouchEvent() of the parent FrameLayout.

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (ev.actionMasked == MotionEvent.ACTION_DOWN){
            startClickTime = System.currentTimeMillis()
            startX = ev.x
            startY = ev.y
        }        
        val allowEvent = (System.currentTimeMillis() - startClickTime) < 1000 && (startX-ev.x).absoluteValue < 15 && (startY-ev.y).absoluteValue < 15
        return !allowEvent
    }

That worked basically, but it occured that after double-tapping the View users are able to scroll by themselves.

Do you have any other ideas to approach this?

Upvotes: 3

Views: 174

Answers (2)

Hyun I Kim
Hyun I Kim

Reputation: 589

You might want to block the user interaction with RecyclerView, not with FrameLayout itself.

Check RecyclerView.OnItemTouchListener.

In your RecyclerView, you can implement OnItemTouchListener and override every method to do nothing.

That will block the user interaction with RecyclerView, making scroll not happen.

Upvotes: 0

lasnow
lasnow

Reputation: 80

Did you try overriding canScrollVertically() method in the LayoutManager?

mLayoutManager = new LinearLayoutManager(getActivity()) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

Edit: Create your own implementation of RecyclerView which it disables the touch event while scrolling is performing. Then you have to change the RecyclerView class in the xml file and Fragment/Activity with it.

Find here an example in Kotlin

class MyRecyclerView : RecyclerView {
    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        return if (scrollState != RecyclerView.SCROLL_STATE_IDLE) false else super.onInterceptTouchEvent(e)
    }
}

And in Java

public class MyRecyclerView extends RecyclerView {
    public MyRecyclerView(Context context) {
        super(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if(getScrollState() != SCROLL_STATE_IDLE)
            return false;
        return super.onInterceptTouchEvent(e);
    }
}

Upvotes: 2

Related Questions