summaryrefslogtreecommitdiff
path: root/library/recyclerview/src/com/android/setupwizardlib/template/RecyclerViewScrollHandlingDelegate.java
blob: 8838c4467210fdb1b97481add0a25da0dfc76e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib.template;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import com.android.setupwizardlib.template.RequireScrollMixin.ScrollHandlingDelegate;

/**
 * {@link ScrollHandlingDelegate} which analyzes scroll events from {@link RecyclerView} and
 * notifies {@link RequireScrollMixin} about scrollability changes.
 */
public class RecyclerViewScrollHandlingDelegate implements ScrollHandlingDelegate {

  private static final String TAG = "RVRequireScrollMixin";

  @Nullable private final RecyclerView recyclerView;

  @NonNull private final RequireScrollMixin requireScrollMixin;

  public RecyclerViewScrollHandlingDelegate(
      @NonNull RequireScrollMixin requireScrollMixin, @Nullable RecyclerView recyclerView) {
    this.requireScrollMixin = requireScrollMixin;
    this.recyclerView = recyclerView;
  }

  private boolean canScrollDown() {
    if (recyclerView != null) {
      // Compatibility implementation of View#canScrollVertically
      final int offset = recyclerView.computeVerticalScrollOffset();
      final int range =
          recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollExtent();
      return range != 0 && offset < range - 1;
    }
    return false;
  }

  @Override
  public void startListening() {
    if (this.recyclerView != null) {
      this.recyclerView.addOnScrollListener(
          new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              requireScrollMixin.notifyScrollabilityChange(canScrollDown());
            }
          });

      if (canScrollDown()) {
        requireScrollMixin.notifyScrollabilityChange(true);
      }
    } else {
      Log.w(TAG, "Cannot require scroll. Recycler view is null.");
    }
  }

  @Override
  public void pageScrollDown() {
    if (recyclerView != null) {
      final int height = recyclerView.getHeight();
      recyclerView.smoothScrollBy(0, height);
    }
  }
}