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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.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
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
index eb1397adf02538a31461c2376a01b0840a9c898d..ac676954f1df5137decf48f11a313c26ab2b53d8 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageRecyclerView.java
@@ -5,7 +5,9 @@
package org.chromium.chrome.browser.ntp.cards;
import android.content.Context;
+import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.GestureDetector;
@@ -19,19 +21,8 @@ import android.view.inputmethod.InputConnection;
* New Tab page receives focus when clicked.
*/
public class NewTabPageRecyclerView extends RecyclerView {
- /**
- * Listener for scroll changes.
- */
- public interface OnScrollListener {
- /**
- * Triggered when the scroll changes. See ScrollView#onScrollChanged for more
- * details.
- */
- void onScrollChanged(int l, int t, int oldl, int oldt);
- }
-
private GestureDetector mGestureDetector;
- private OnScrollListener mOnScrollListener;
+ private LinearLayoutManagerWithSmoothScroller mLayoutManager;
/**
* Constructor needed to inflate from XML.
@@ -48,7 +39,12 @@ public class NewTabPageRecyclerView extends RecyclerView {
return retVal;
}
});
- setLayoutManager(new LinearLayoutManager(getContext()));
+ mLayoutManager = new LinearLayoutManagerWithSmoothScroller(getContext());
+ setLayoutManager(mLayoutManager);
+ }
+
+ public boolean isFirstItemVisible() {
+ return mLayoutManager.findFirstVisibleItemPosition() == 0;
}
@Override
@@ -66,20 +62,6 @@ public class NewTabPageRecyclerView extends RecyclerView {
return super.onTouchEvent(ev);
}
- /**
- * Sets the listener to be notified of scroll changes.
- * @param listener The listener to be updated on scroll changes.
- */
- public void setOnScrollListener(OnScrollListener listener) {
- mOnScrollListener = listener;
- }
-
- @Override
- protected void onScrollChanged(int l, int t, int oldl, int oldt) {
- super.onScrollChanged(l, t, oldl, oldt);
- if (mOnScrollListener != null) mOnScrollListener.onScrollChanged(l, t, oldl, oldt);
- }
-
@Override
public void focusableViewAvailable(View v) {
// To avoid odd jumps during NTP animation transitions, we do not attempt to give focus
@@ -90,8 +72,36 @@ public class NewTabPageRecyclerView extends RecyclerView {
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
- // Fixes lanscape transitions when unfocusing the URL bar: crbug.com/288546
+ // Fixes landscape transitions when unfocusing the URL bar: crbug.com/288546
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
return super.onCreateInputConnection(outAttrs);
}
+
+ private static class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {
+ private final Context mContext;
+
+ public LinearLayoutManagerWithSmoothScroller(Context context) {
+ super(context);
+ mContext = context;
+ }
+
+ @Override
+ public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
+ int position) {
+ LinearSmoothScroller scroller = new LinearSmoothScroller(mContext) {
+ @Override
+ public PointF computeScrollVectorForPosition(int targetPosition) {
+ return LinearLayoutManagerWithSmoothScroller.this
+ .computeScrollVectorForPosition(targetPosition);
+ }
+
+ @Override
+ protected int getVerticalSnapPreference() {
+ return SNAP_TO_START;
+ }
+ };
+ scroller.setTargetPosition(position);
+ startSmoothScroll(scroller);
+ }
+ }
}

Powered by Google App Engine
This is Rietveld 408576698