1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.guide; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 22 public class TimelineRow extends TimelineGridView { 23 private static final float FADING_EDGE_STRENGTH_START = 1.0f; 24 25 private int mScrollPosition; 26 TimelineRow(Context context)27 public TimelineRow(Context context) { 28 this(context, null); 29 } 30 TimelineRow(Context context, AttributeSet attrs)31 public TimelineRow(Context context, AttributeSet attrs) { 32 this(context, attrs, 0); 33 } 34 TimelineRow(Context context, AttributeSet attrs, int defStyle)35 public TimelineRow(Context context, AttributeSet attrs, int defStyle) { 36 super(context, attrs, defStyle); 37 } 38 resetScroll()39 public void resetScroll() { 40 getLayoutManager().scrollToPosition(0); 41 } 42 43 /** Returns the current scroll position */ getScrollOffset()44 public int getScrollOffset() { 45 return Math.abs(mScrollPosition); 46 } 47 48 /** Scrolls horizontally to the given position. */ scrollTo(int scrollOffset, boolean smoothScroll)49 public void scrollTo(int scrollOffset, boolean smoothScroll) { 50 int dx = 51 (scrollOffset - getScrollOffset()) 52 * (getLayoutDirection() == LAYOUT_DIRECTION_LTR ? 1 : -1); 53 if (smoothScroll) { 54 smoothScrollBy(dx, 0); 55 } else { 56 scrollBy(dx, 0); 57 } 58 } 59 60 @Override onRtlPropertiesChanged(int layoutDirection)61 public void onRtlPropertiesChanged(int layoutDirection) { 62 super.onRtlPropertiesChanged(layoutDirection); 63 // Reset scroll 64 if (isAttachedToWindow()) { 65 scrollTo(getScrollOffset(), false); 66 } 67 } 68 69 @Override onScrolled(int dx, int dy)70 public void onScrolled(int dx, int dy) { 71 if (dx == 0 && dy == 0) { 72 mScrollPosition = 0; 73 } else { 74 mScrollPosition += dx; 75 } 76 } 77 78 @Override getLeftFadingEdgeStrength()79 protected float getLeftFadingEdgeStrength() { 80 return (getLayoutDirection() == LAYOUT_DIRECTION_LTR) ? FADING_EDGE_STRENGTH_START : 0; 81 } 82 83 @Override getRightFadingEdgeStrength()84 protected float getRightFadingEdgeStrength() { 85 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ? FADING_EDGE_STRENGTH_START : 0; 86 } 87 } 88