Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java

Issue 1884883004: Implement snap scrolling for Zine NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java
index dd0955ea9d8b623d1853ef9bc06b8e299aec8368..651d1867bff406c00981b648ed8814a213cfddf9 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageView.java
@@ -415,8 +415,15 @@ public class NewTabPageView extends FrameLayout
// percentage if some basic view properties are sane.
View wrapperView = mUseCardsUi ? mRecyclerView : mScrollView;
if (wrapperView.getHeight() != 0 && mSearchBoxView.getTop() != 0) {
- int scrollY = getVerticalScroll();
- percentage = Math.max(0f, Math.min(1f, scrollY / (float) mSearchBoxView.getTop()));
+ // getVerticalScroll is valid only for the RecyclerView if the first item is visible.
+ // Luckily, if the first item is not visible, we know the toolbar transition should
+ // be 100%.
+ if (mUseCardsUi && !mRecyclerView.isFirstItemVisible()) {
+ percentage = 1f;
+ } else {
+ int scrollY = getVerticalScroll();
+ percentage = Math.max(0f, Math.min(1f, scrollY / (float) mSearchBoxView.getTop()));
+ }
}
updateVisualsForToolbarTransition(percentage);
@@ -427,12 +434,63 @@ public class NewTabPageView extends FrameLayout
}
private void initializeSearchBoxRecyclerViewScrollHandling() {
+ final Runnable mSnapScrollRunnable = new Runnable() {
+ @Override
+ public void run() {
+ assert mPendingSnapScroll;
+
+ // These calculations only work if the first item is visible (since
+ // computeVerticalScrollOffset only takes into account visible items).
+ // Luckily, we only need to perform the calculations if the first item is visible.
+ if (!mRecyclerView.isFirstItemVisible()) return;
+ int currentScroll = mRecyclerView.computeVerticalScrollOffset();
+
+ // Scroll to hide all of content view off the top of the page.
+ // We subtract mContentView.getPaddingTop() to offset the contents below the
+ // search box.
+ int targetScroll = mContentView.getHeight() - mContentView.getPaddingTop();
May 2016/04/15 12:33:55 Why do we use pixels to indicate where to scroll t
PEConn 2016/04/15 12:50:32 I did originally try this. Part of the problem is
+
+ if (currentScroll > 0 && currentScroll < targetScroll) {
+ if (currentScroll < mSearchBoxView.getTop()) {
+ // Scroll to the top.
+ mRecyclerView.smoothScrollBy(0, -currentScroll);
+ } else {
+ // Scroll to the articles.
+ mRecyclerView.smoothScrollBy(0, targetScroll - currentScroll);
+ }
+ }
+
+ mPendingSnapScroll = false;
+ }
+ };
+
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+ if (mPendingSnapScroll) {
+ mRecyclerView.removeCallbacks(mSnapScrollRunnable);
+ mRecyclerView.postDelayed(mSnapScrollRunnable, SNAP_SCROLL_DELAY_MS);
+ }
updateSearchBoxOnScroll();
}
});
+
+ mRecyclerView.setOnTouchListener(new OnTouchListener() {
+ @Override
+ @SuppressLint("ClickableViewAccessibility")
+ public boolean onTouch(View v, MotionEvent event) {
+ mRecyclerView.removeCallbacks(mSnapScrollRunnable);
+
+ if (event.getActionMasked() == MotionEvent.ACTION_CANCEL
+ || event.getActionMasked() == MotionEvent.ACTION_UP) {
+ mPendingSnapScroll = true;
+ mRecyclerView.postDelayed(mSnapScrollRunnable, SNAP_SCROLL_DELAY_MS);
+ } else {
+ mPendingSnapScroll = false;
+ }
+ return false;
+ }
+ });
}
private void initializeSearchBoxScrollHandling() {
@@ -462,7 +520,6 @@ public class NewTabPageView extends FrameLayout
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
- if (mScrollView.getHandler() == null) return false;
mScrollView.removeCallbacks(mSnapScrollRunnable);
if (event.getActionMasked() == MotionEvent.ACTION_CANCEL
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698