Home | History | Annotate | Download | only in guide
      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 
     27     public TimelineRow(Context context) {
     28         this(context, null);
     29     }
     30 
     31     public TimelineRow(Context context, AttributeSet attrs) {
     32         this(context, attrs, 0);
     33     }
     34 
     35     public TimelineRow(Context context, AttributeSet attrs, int defStyle) {
     36         super(context, attrs, defStyle);
     37     }
     38 
     39     public void resetScroll() {
     40         getLayoutManager().scrollToPosition(0);
     41     }
     42 
     43     /**
     44      * Returns the current scroll position
     45      */
     46     public int getScrollOffset() {
     47         return Math.abs(mScrollPosition);
     48     }
     49 
     50     /**
     51      * Scrolls horizontally to the given position.
     52      */
     53     public void scrollTo(int scrollOffset, boolean smoothScroll) {
     54         int dx = (scrollOffset - getScrollOffset())
     55                 * (getLayoutDirection() == LAYOUT_DIRECTION_LTR ? 1 : -1);
     56         if (smoothScroll) {
     57             smoothScrollBy(dx, 0);
     58         } else {
     59             scrollBy(dx, 0);
     60         }
     61     }
     62 
     63     @Override
     64     public void onRtlPropertiesChanged(int layoutDirection) {
     65         super.onRtlPropertiesChanged(layoutDirection);
     66         // Reset scroll
     67         if (isAttachedToWindow()) {
     68             scrollTo(getScrollOffset(), false);
     69         }
     70     }
     71 
     72     @Override
     73     public void onScrolled(int dx, int dy) {
     74         if (dx == 0 && dy == 0) {
     75             mScrollPosition = 0;
     76         } else {
     77             mScrollPosition += dx;
     78         }
     79     }
     80 
     81     @Override
     82     protected float getLeftFadingEdgeStrength() {
     83         return (getLayoutDirection() == LAYOUT_DIRECTION_LTR) ? FADING_EDGE_STRENGTH_START : 0;
     84     }
     85 
     86     @Override
     87     protected float getRightFadingEdgeStrength() {
     88         return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ? FADING_EDGE_STRENGTH_START : 0;
     89     }
     90 }
     91