android-tv-vertical scrolling

Source: Internet
Author: User
<span id="Label3"></p><p><p>Vertical scrolling:</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"> public class newscrollview extends scrollview { public newscrollview ( Context context)  {  super (context);  } public newscrollview (Context  Context, attributeset attrs)  {  super (context, attrs); } public  Newscrollview (context context, attributeset attrs, int defstyleattr)  {   super (context, attrs, defstyleattr); }  @Override  protected int  Computescrolldeltatogetchildrectonscreen (rect rect)  {  if  (getchildcount ()  ==  0)    return 0;  int height = getheight ();   int  screentop = getscrolly ();  int screenbottom = screentop +  height;  int fadingedge = 100;  if  (rect.top > 0)  {    screentop += fadingedge;  }  if  (rect.bottom < getchildat (0). getHeight ())  {   screenBottom -= fadingEdge;  }  //  int  scrollydelta = 0;  if  (rect.bottom > screenbottom &&  RECT.TOP&NBSP;&GT;&NBSP;SCREENTOP)  {   if  (rect.height ()  > height)   {    scrollydelta +=  (rect.top - screentop);   }  else {    scrollydelta +=  (rect.bottom - screenbottom);    }   int bottom = getchildat (0). getbottom ();    int  distanceToBottom = bottom - screenBottom;   scrollYDelta =  Math.min (scrollydelta, distancetobottom);  } else if  (rect.top <  screentop && rect.bottom < screenbottom)  {   if  (rect.height ()  >  Height)  {    scrollYDelta -=  (screenbottom - rect.bottom);    } else {    scrollYDelta -=  (screentop -  rect.top);    }   scrollydelta = math.max (scrollYDelta, - getscrolly ());   }  return scrollydelta; }</pre> <p><p></p></p> <p><p>Horizontal scrolling (original code, xiaomi):</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"> public class SmoothHorizontalScrollView extends HorizontalScrollView {  final string tag =  "smoothhorizontalscrollview"; public  Smoothhorizontalscrollview (context context)  {  this (context, null, 0);  }  public smoothhorizontalscrollview (context context, attributeset attrs)  {   this (context, attrs, 0);  } public smoothhorizontalscrollview (Context  Context, attributeset attrs,   int defstyle)  {  super (context,  attrs, defstyle); }  @Override  public void scrollby (int dx, int  dy)  {  super.scrollby (dx, dy); }  @Override  public void  Computescroll ()  {  super.computescroll (); } private int _index =  -1; public void settabindex (int indEx)  {  _index = index; } private int _tabcount = -1;  public void settabcount (int _count)  {  _tabcount = _count;  }  @Override  public boolean dispatchkeyevent (keyevent event)  {  view  currentfocused = findfocus ();  if  (currentfocused == this)     currentFocused = null;  if  (event.getkeycode ()  ==  Keyevent.keycode_dpad_left    && event.getaction ()  ==  Keyevent.action_down)  {   view nextfocused = focusfinder.getinstance (). Findnextfocus (this,     currentfocused, view.focus_left);    if   (nextfocused == null && _index == 0)  {     object obj = currentfocused       .gettag (r.integer.tag_view_focused_host_view);    if  ( MetroCursorView.class.isInstance (obj))  {     utils.playkeysound ((metrocursorview )  obj,       utils.sound_error_key);      ( Metrocursorview)  obj). Showindicator ();    }    return true;    }   /*    * } else if (event.getKeyCode ()  == keyevent.keycode_dpad_down &&    * event.getaction ()   == keyevent.action_down) { view nextfocused =    *  Focusfinder.getinstance (). Findnextfocus (this, currentfocused,    * view.focus_ down);  if (nextfocused == null) { object obj =    *  Currentfocused.gettag (R.INTEGER.TAG_VIEW_FOcused_host_view);     * if (MetroCursorView.class.isInstance (obj)) {     * utils.playkeysound ((metrocursorview) obj, utils.sound_error_key);     *   ((metrocursorview) obj). showindicator ();  } log.d (tag,    *  "no  Move to down "); }    */  } else if  ( Event.getkeycode ()  == KeyEvent.KEYCODE_DPAD_RIGHT    &&  Event.getaction ()  == keyevent.action_down)  {   view nextfocused =  focusfinder.getinstance (). Findnextfocus (this,     currentfocused,  view.focus_right);   if  (nextfocused == null)  {     Object obj = currentfocused      .gettag (R.integer.tag_view_ focused_host_view);    if  (metrocursorvIew.class.isInstance (obj))  {     if  (_tabcount != -1 & & _index == _tabcount - 1)  {       Utils.playkeysound ((metrocursorview)  obj,        utils.sound_ error_key);       ((metrocursorview)  obj). showindicator ();       return true;     }    }    &NBSP;LOG.D (tag,  "no move to right");    }  }  boolean  ret = super.dispatchkeyevent (event);  if  (ret == true     &&  (event.getkeycode ()  == KeyEvent.KEYCODE_DPAD_RIGHT       && event.getaction ()  == keyevent.action_down | |  event      .getkeycoDe ()  == KeyEvent.KEYCODE_DPAD_LEFT      &&  Event.getaction () &NBSP;==&NBSP;KEYEVENT.ACTION_DOWN))  {   utils.playkeysound (this,  utils.sound_keystone_key);  }  return ret; }  @Override  protected  Int computescrolldeltatogetchildrectonscreen (rect rect)  {  if  (getchildcount ()  == 0)    return 0;  int width = getwidth ();   INT&NBSP;SCREENLEFT&NBSP;=&NBSP;GETSCROLLX ();   int screenright = screenleft +  width;  int fadingedge = this.getresources (). getdimensionpixelsize (     r.dimen.fading_edge);   // leave room for left fading edge  as long as rect isn ' t at very left  if  (rect.left  > 0)  {   screenleft += fadingedge;  }  // leave room for right  fading edge as long as rect isn ' t at very right   if  (rect.right < getchildat (0). getwidth ())  {   screenRight -=  fadingedge;  }  int scrollxdelta = 0;  if  (rect.right  > screenright && rect.left > screenleft)  {   //  need to move right to get it in view: move right  Just enough so   // that the entire rectangle is in  view  (or at least the first   // screen size  Chunk) .   if  (rect.width ()  > width)  {    //  Just enough to get screen size chunk on    scrollxdelta +=  (rect.left  - screenleft);    } else {    // get entire  rect at right of screen    scrollXDelta +=  (rect.right  - screenright);    }   // make sure we aren ' t  scrolling beyond the end of our content   int right  = getchildat (0). getRight ();    int distancetoright = right -  screenright;   scrollxdelta = math.min (scrollxdelta, distancetoright);   } else if  (rect.left < screenleft && rect.right  < screenright)  {   // need to move right to  Get it in view: move right just enough so   // that   // entire  rectangle is in view  (or at least the first screen    // size chunk of it) .   if  (rect.width ()  > width)  {    // screen size chunk    scrollxdelta -=   (screenright - rect.right);    } else {    //  entire rect at left    scrollXDelta -=  (screenleft -  rect.left);    }   // make sure we aren ' t  scrolling any further than the left our   // content    scrollxdelta = math.max (SCROLLXDELTA,&NBSP;-GETSCROLLX ());  }   return scrollxdelta; }  @Override  protected boolean onrequestfocusindescendants (int direction,    Rect previouslyfocusedrect)  {  // convert from forward / backward  notation to up / down / left / right  //  (ugh).   if  (previouslyfocusedrect != null)  {   if  (direction  == view.focus_forward)  {    direction = View.FOCUS_RIGHT;    } else if  (direction == view.focus_backward)  {     direction = view.focus_left;   }   view nextfocus =  Focusfinder.getinstance (). Findnextfocusfromrect (     this, previouslyfocusedrect ,  direction);   if  (nextfocus == null)  {    return  false; &nbSp; }   return nextfocus.requestfocus (direction, previouslyfocusedrect);   } else {   int index;   int increment;    int end;   int count = this.getchildcount ();    if   ((direction & focus_forward)  != 0)  {    index =  0;    increment = 1;    end = count;    } else {    index = count - 1;     increment = -1;    end = -1;   }    for  (int i = index; i != end; i += increment)  {     view child = this.getchildat (i);    if  ( Child.getvisibility ()  == view. VISIBLE)  {     if  (child.requestfocus (direction, previouslyfocusedrect ))  {      return true;     }     }   }   return false;  } }}</pre> <p><p></p></p> <p><p></p></p><p><p>android-tv-vertical scrolling</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.