Help us understand the problem. What is going on with this article?

RecyclerViewのスクロールの可・不可を制御する

More than 3 years have passed since last update.

RecyclerViewでのスクロールを不可能にし、任意のタイミングでスクロール可能にしたい場合のメモ。

RecyclerView#OnItemTouchListenerを実装

まずRecyclerViewのOnItemTouchListenerを実装したクラスを用意します。

public class ScrollController implements RecyclerView.OnItemTouchListener {

    public ScrollController() {
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

onInterceptTouchEventがtrueを返すよう変更

onInterceptTouchEventがtrueを返すことでRecyclerViewのスクロールが無効になります。

public class ScrollController implements RecyclerView.OnItemTouchListener {

    public ScrollController() {
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return true;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

使い方はRecyclerViewにaddOnItemTouchListenerで追加するだけです。

RecyclerView rv = new RecyclerView(getContext());
ScrollController controller = new ScrollController()
rv.addOnItemTouchListener(controller);

再びスクロール可能にするには、removeOnItemTouchListenerを使います。

rv.removeOnItemTouchListener(controller);

使いやすくする

スクロールの可・不可を制御するためにaddとremoveを繰り返すのを避けたいので、メソッドを用意し動的にonInterceptTouchEventの戻り値を変えられるようにします。

public class ScrollController implements RecyclerView.OnItemTouchListener {

    private boolean isDisableScroll;

    public ScrollController() {
    }

    public void disableScroll() {
        isDisableScroll = true;
    }

    public void enableScroll() {
        isDisableScroll = false;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return isDisableScroll;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

これでaddOnItemTouchListenerで一度コントローラーを追加するだけで済み、スクロールを可にする場合にremoveOnItemTouchListenerの実行が不要になります。

RecyclerView rv = new RecyclerView(getContext());
ScrollController controller = new ScrollController()
rv.addOnItemTouchListener(controller);

// スクロール不可
controller.disableScroll();

// スクロール可
controller.enableScroll();

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away