Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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);
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda