summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/util
diff options
context:
space:
mode:
authorMaurice Lam <yukl@google.com>2016-01-22 17:15:40 -0800
committerMaurice Lam <yukl@google.com>2016-01-26 17:51:23 -0800
commit7bc6f176937ed369b180fa89f6c311d2801f206c (patch)
treec0094237308b149febfc17c2194c23d506b8c744 /library/main/src/com/android/setupwizardlib/util
parentd6fe3672c84594bf53882eb5ac5c26bbd6b714e7 (diff)
downloadsetupwizard-7bc6f176937ed369b180fa89f6c311d2801f206c.tar.gz
[SuwLib] Allow requiring scroll in lists
Add ListViewRequireScrollHelper and RecyclerViewRequireScrollHelper that will require scrolling in those layouts. SetupWizardListLayout and SetupWizardRecyclerItemsLayout are now wired properly with the requireScrollToBottom() method to require scrolling. Change-Id: I1086fe02374bfbbeb1ac3f8c4ca6981f3ced0df9
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/util')
-rw-r--r--library/main/src/com/android/setupwizardlib/util/AbstractRequireScrollHelper.java78
-rw-r--r--library/main/src/com/android/setupwizardlib/util/ListViewRequireScrollHelper.java81
-rw-r--r--library/main/src/com/android/setupwizardlib/util/RequireScrollHelper.java50
3 files changed, 173 insertions, 36 deletions
diff --git a/library/main/src/com/android/setupwizardlib/util/AbstractRequireScrollHelper.java b/library/main/src/com/android/setupwizardlib/util/AbstractRequireScrollHelper.java
new file mode 100644
index 0000000..2697371
--- /dev/null
+++ b/library/main/src/com/android/setupwizardlib/util/AbstractRequireScrollHelper.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2016 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.util;
+
+import android.view.View;
+
+import com.android.setupwizardlib.view.NavigationBar;
+
+/**
+ * Add this helper to require the scroll view to be scrolled to the bottom, making sure that the
+ * user sees all content on the screen. This will change the navigation bar to show the more button
+ * instead of the next button when there is more content to be seen. When the more button is
+ * clicked, the scroll view will be scrolled one page down.
+ */
+public abstract class AbstractRequireScrollHelper implements View.OnClickListener {
+
+ private final NavigationBar mNavigationBar;
+
+ private boolean mScrollNeeded;
+ // Whether the user have seen the more button yet.
+ private boolean mScrollNotified = false;
+
+ protected AbstractRequireScrollHelper(NavigationBar navigationBar) {
+ mNavigationBar = navigationBar;
+ }
+
+ protected void requireScroll() {
+ mNavigationBar.getMoreButton().setOnClickListener(this);
+ }
+
+ protected void notifyScrolledToBottom() {
+ if (mScrollNeeded) {
+ mNavigationBar.post(new Runnable() {
+ @Override
+ public void run() {
+ mNavigationBar.getNextButton().setVisibility(View.VISIBLE);
+ mNavigationBar.getMoreButton().setVisibility(View.GONE);
+ }
+ });
+ mScrollNeeded = false;
+ mScrollNotified = true;
+ }
+ }
+
+ protected void notifyRequiresScroll() {
+ if (!mScrollNeeded && !mScrollNotified) {
+ mNavigationBar.post(new Runnable() {
+ @Override
+ public void run() {
+ mNavigationBar.getNextButton().setVisibility(View.GONE);
+ mNavigationBar.getMoreButton().setVisibility(View.VISIBLE);
+ }
+ });
+ mScrollNeeded = true;
+ }
+ }
+
+ @Override
+ public void onClick(View view) {
+ pageScrollDown();
+ }
+
+ protected abstract void pageScrollDown();
+}
diff --git a/library/main/src/com/android/setupwizardlib/util/ListViewRequireScrollHelper.java b/library/main/src/com/android/setupwizardlib/util/ListViewRequireScrollHelper.java
new file mode 100644
index 0000000..7877569
--- /dev/null
+++ b/library/main/src/com/android/setupwizardlib/util/ListViewRequireScrollHelper.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 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.util;
+
+import android.os.Build;
+import android.widget.AbsListView;
+import android.widget.ListAdapter;
+import android.widget.ListView;
+
+import com.android.setupwizardlib.view.NavigationBar;
+
+/**
+ * Add this helper to require the list view to be scrolled to the bottom, making sure that the
+ * user sees all content on the screen. This will change the navigation bar to show the more button
+ * instead of the next button when there is more content to be seen. When the more button is
+ * clicked, the list view will be scrolled one page down.
+ */
+public class ListViewRequireScrollHelper extends AbstractRequireScrollHelper
+ implements AbsListView.OnScrollListener {
+
+ public static void requireScroll(NavigationBar navigationBar, ListView listView) {
+ new ListViewRequireScrollHelper(navigationBar, listView).requireScroll();
+ }
+
+ private final ListView mListView;
+
+ private ListViewRequireScrollHelper(NavigationBar navigationBar, ListView listView) {
+ super(navigationBar);
+ mListView = listView;
+ }
+
+ @Override
+ protected void requireScroll() {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
+ // APIs to scroll a list only exists on Froyo or above.
+ super.requireScroll();
+ mListView.setOnScrollListener(this);
+
+ final ListAdapter adapter = mListView.getAdapter();
+ if (mListView.getLastVisiblePosition() < adapter.getCount()) {
+ notifyRequiresScroll();
+ }
+ }
+ }
+
+ @Override
+ protected void pageScrollDown() {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
+ final int height = mListView.getHeight();
+ mListView.smoothScrollBy(height, 500);
+ }
+ }
+
+ @Override
+ public void onScrollStateChanged(AbsListView view, int scrollState) {
+ }
+
+ @Override
+ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+ int totalItemCount) {
+ if (firstVisibleItem + visibleItemCount >= totalItemCount) {
+ notifyScrolledToBottom();
+ } else {
+ notifyRequiresScroll();
+ }
+ }
+}
diff --git a/library/main/src/com/android/setupwizardlib/util/RequireScrollHelper.java b/library/main/src/com/android/setupwizardlib/util/RequireScrollHelper.java
index 8d796d7..cce336f 100644
--- a/library/main/src/com/android/setupwizardlib/util/RequireScrollHelper.java
+++ b/library/main/src/com/android/setupwizardlib/util/RequireScrollHelper.java
@@ -16,7 +16,6 @@
package com.android.setupwizardlib.util;
-import android.view.View;
import android.widget.ScrollView;
import com.android.setupwizardlib.view.BottomScrollView;
@@ -28,59 +27,38 @@ import com.android.setupwizardlib.view.NavigationBar;
* instead of the next button when there is more content to be seen. When the more button is
* clicked, the scroll view will be scrolled one page down.
*/
-public class RequireScrollHelper implements BottomScrollView.BottomScrollListener,
- View.OnClickListener {
+public class RequireScrollHelper extends AbstractRequireScrollHelper
+ implements BottomScrollView.BottomScrollListener {
- /**
- * Require scrolling on the scrollView, so that if the scrollView has content hidden beneath the
- * fold, the next button will be hidden and the more button will be shown instead. The more
- * button will scroll the scrollView downwards when clicked until the bottom is reached.
- *
- * @param navigationBar The navigation bar in which the next button's label will be changed.
- * @param scrollView The {@link BottomScrollView} to be scrolled.
- */
- public static RequireScrollHelper requireScroll(NavigationBar navigationBar,
- BottomScrollView scrollView) {
- final RequireScrollHelper helper = new RequireScrollHelper(navigationBar, scrollView);
- helper.requireScroll();
- return helper;
+ public static void requireScroll(NavigationBar navigationBar, BottomScrollView scrollView) {
+ new RequireScrollHelper(navigationBar, scrollView).requireScroll();
}
private final BottomScrollView mScrollView;
- private final NavigationBar mNavigationBar;
-
- private boolean mScrollNeeded;
private RequireScrollHelper(NavigationBar navigationBar, BottomScrollView scrollView) {
- mNavigationBar = navigationBar;
+ super(navigationBar);
mScrollView = scrollView;
}
- private void requireScroll() {
- mNavigationBar.getMoreButton().setOnClickListener(this);
+ @Override
+ protected void requireScroll() {
+ super.requireScroll();
mScrollView.setBottomScrollListener(this);
}
@Override
- public void onScrolledToBottom() {
- if (mScrollNeeded) {
- mNavigationBar.getNextButton().setVisibility(View.VISIBLE);
- mNavigationBar.getMoreButton().setVisibility(View.GONE);
- mScrollNeeded = false;
- }
+ protected void pageScrollDown() {
+ mScrollView.pageScroll(ScrollView.FOCUS_DOWN);
}
@Override
- public void onRequiresScroll() {
- if (!mScrollNeeded) {
- mNavigationBar.getNextButton().setVisibility(View.GONE);
- mNavigationBar.getMoreButton().setVisibility(View.VISIBLE);
- mScrollNeeded = true;
- }
+ public void onScrolledToBottom() {
+ notifyScrolledToBottom();
}
@Override
- public void onClick(View view) {
- mScrollView.pageScroll(ScrollView.FOCUS_DOWN);
+ public void onRequiresScroll() {
+ notifyRequiresScroll();
}
}