Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What is going on with this article?
@Horie1024

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

More than 5 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();

参考

20
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  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
zozotech
70億人のファッションを技術の力で変えていく

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
20
Help us understand the problem. What is going on with this article?