Android realizes reading app panning page effect _android

Source: Internet
Author: User
Tags abs sca touch

One of their own app needs to use the page to read, the Internet to see the effect of stereo paging, but there are too many bugs are not compatible. Look at the reading read the page is a translation of the Flip page, so that The imitation of a translation of the page to the control. The effect is as follows:

A shadow is drawn on the right edge of the page, and the effect is good. It's not difficult to implement this panning control, just define a layout management page. Specific implementation of the following difficulties:

1, Cycle page, the repeated use of pages.

2, in the page when filtering out the touch of many points.

3, the use of Setadapter to set the layout of the page and data.

Here are one by one to solve these difficulties. First look at the issue of circular paging, how can you use less pages to achieve this page? Because the screen can only display a complete page, turn over the page is not visible, so you can turn over the page to reuse, do not have to be a new page every time, so, I only used three pages to realize the cycle page. To reuse a page, you first need to know the number of pages in the layout and the corresponding hierarchical relationship, such as a parent control's child view of the larger number is located in the upper. The schematic diagram of the recycled page is as follows:

When the page is turned right, the state diagram is like this, only used 0, 1, 23 pages, page number 2 is located at the top, I hid it on the left, so see only page 1, page 0 under 1 blocked also see, page right when pages 2 is sliding to the screen, At this time the content of the page 0 to replace the page 2 of the previous page content, put it to the previous page 2 position, then, the state returned to the initial state, and can continue to the right page!

When the page is left, the initial state is the same, when Page 1 is turned left out, see is the page 0, this time page 0 there is no page, and the page 2 has been used, this time the page 2 placed under the page 0, this time the state is back to the initial state, you can continue to the left page.

Similar to the implementation of this loop effect I have always used the solution is to put the selected in the middle, such as the schematic of the page 1, each page after the completion of the visible is 1 pages. This is the same scenario in the scrolling selector Pickerview . This solves the problem of reusing the page.

Solve the difficulty of 2 page when the filter more touch this problem in imitation Taobao product browsing interface has been resolved, is to use a control variable mevents filter out pointer down or up after the arrival of the first move event.

Solve the problem 3 use adapter way to set the layout and data of the page. This is in the Android Adapterview, but I did not see its adapter mechanism, too complex, I have a simple adapter, as follows:

Pageadapter.java:

Package Com.jingchen.pagerdemo; 
 
Import Android.view.View; 
 
Public abstract class PageAdapter 
{ 
 /** 
  * @return page View 
 /public abstract View GetView (); 
 
 public abstract int GetCount (); 
 
 /** 
  * Add content to view * * 
  @param view *   contains content view 
  * @param position 
  *   Position page 
  * 
 /public abstract void addcontent (view view, int position); 

This is an abstract class, GetView () is used to return the layout of the page, GetCount () the total number of pages required to return the data, addcontent (view view, int position) which will be called to request page data after each page is turned over. The argument view is the page, and the position is the first few pages. Later, you will define the Setadapter method settings in the custom layout to set the adapter.
OK, the difficulty is solved, custom a layout called Scanview inherited from Relativelayout:

Scanview.java:

Package Com.jingchen.pagerdemo; 
Import Java.util.Timer; 
 
Import Java.util.TimerTask; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import android.graphics.LinearGradient; 
Import Android.graphics.Paint; 
Import Android.graphics.Paint.Style; 
Import Android.graphics.RectF; 
Import Android.graphics.Shader.TileMode; 
Import Android.os.Handler; 
Import Android.os.Message; 
Import Android.util.AttributeSet; 
Import Android.util.Log; 
Import android.view.MotionEvent; 
Import Android.view.VelocityTracker; 
Import Android.view.View; 
 
Import Android.widget.RelativeLayout; /** * @author chenjing * */public class Scanview extends Relativelayout {public static final String TAG = "Sca 
 Nview "; 
 Private Boolean isinit = true; 
 There are two pages to slide when sliding, to determine which page is sliding private Boolean ispremoving = True, iscurrmoving = true; 
 is currently the first few pages of private int index; 
 private float lastx; 
 Previous page, current page, left position of next page private int prepageleft = 0, Currpageleft = 0, nextpageleft = 0; // Three page private View prepage, Currpage, nextPage; 
 Page state private static final int state_move = 0; 
 private static final int state_stop = 1; 
 Slide the page, only the previous page and the current page can be slippery private static final int PRE = 2; 
 private static final int CURR = 3; 
 private int state = State_stop; 
 The right position of the page being slid, used to draw the shadow private float rights; 
 Fingers sliding distance private float movelenght; 
 Page wide high private int mwidth, mheight; 
 Get sliding speed private velocitytracker VT; 
 Prevent dithering private float speed_shake = 20; 
 Current sliding speed private float speed; 
 private timer timer; 
 Private Mytimertask Mtask; 
 Moving speed of the sliding animation public static final int move_speed = 10; 
 Page adapter private PageAdapter adapter; 
 
 /** * Filter Multiple TOUCH control variable * * private int mevents; 
 public void Setadapter (Scanviewadapter adapter) {removeallviews (); 
 This.adapter = adapter; 
 Prepage = Adapter.getview (); 
 AddView (prepage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); Adapter.addcontent (Prepage, index-1);
 
 Currpage = Adapter.getview (); 
 AddView (currpage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 
 Adapter.addcontent (currpage, index); 
 NextPage = Adapter.getview (); 
 AddView (nextPage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 
 Adapter.addcontent (nextPage, index + 1); /** * Slide to the left. Note the pages that can be slid are only the current page and the previous page * * @param which */private void moveLeft (int which) {switch (which) {case Pre:pre 
 Pageleft-= Move_speed; 
 if (Prepageleft <-mwidth) Prepageleft =-mwidth; 
 right = Mwidth + Prepageleft; 
 Break 
 Case Curr:currpageleft-= move_speed; 
 if (Currpageleft <-mwidth) Currpageleft =-mwidth; 
 right = Mwidth + Currpageleft; 
 Break }/** * slide to the right. Note the pages that can be slid are only the current page and the previous page * * @param which */private void moveright (int which) {switch (which) {case PRE:PR 
 Epageleft + = Move_speed; 
 if (Prepageleft > 0) prepageleft = 0; right = Mwidth + PrepageleFt 
 Break 
 Case Curr:currpageleft + = Move_speed; 
 if (Currpageleft > 0) currpageleft = 0; 
 right = Mwidth + Currpageleft; 
 Break 
 }/** * When you turn back a page, add the previous page to the left/private void Addprepage () {Removeview (nextPage); 
 AddView (NextPage,-1, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 Get the previous page content from the adapter adapter.addcontent (NextPage, index-1); 
 Exchange Order View temp = nextPage; 
 NextPage = Currpage; 
 Currpage = Prepage; 
 Prepage = temp; 
 Prepageleft =-mwidth; 
 /** * When you turn a page forward, add a page at the bottom/private void Addnextpage () {Removeview (prepage); 
 AddView (prepage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 Get the next page content from the adapter adapter.addcontent (prepage, index + 1); 
 Exchange Order View temp = currpage; 
 Currpage = NextPage; 
 NextPage = Prepage; 
 Prepage = temp; 
 Currpageleft = 0; 
 Handler Updatehandler = new Handler () {@Override public void Handlemessage (msg){if (state!= state_move) return; Move the page//flip back, first determine which page is currently in the not returned status if (Prepageleft >-mwidth && speed <= 0) {//previous page is not returned in status MoveLeft (PR 
 E); 
 else if (Currpageleft < 0 && speed >= 0) {//The current page is not in the returned state MoveRight (CURR); 
 else if (Speed < 0 && index < Adapter.getcount ()) {//left turn, flip is the current page moveLeft (CURR); 
  if (Currpageleft = = (-mwidth)) {index++; 
 Turn over one page and add a page underneath to remove the top page addnextpage (); 
 } else if (Speed > 0 && index > 1) {//Turn right and flip the previous page moveright (pre); 
  if (Prepageleft = = 0) {index--; 
 Turn back a page, add a page to the top, hide in the leftmost addprepage (); 
 } if (right = = 0 | | right = = mwidth) {releasemoving (); 
 state = State_stop; 
 Quitmove (); 
 } ScanView.this.requestLayout (); 
 
 } 
 
 }; 
 Public Scanview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 Init (); 
 Public Scanview {Super (context); 
 Init (); } PUBlic Scanview (context, AttributeSet attrs) {Super (context, attrs); 
 Init (); 
 /** * Exit Animation page */public void Quitmove () {if (mtask!= null) {mtask.cancel (); 
 Mtask = null; 
 } private void Init () {index = 1; 
 Timer = new timer (); 
 Mtask = new Mytimertask (Updatehandler); 
 /** * Release action, do not restrict hand sliding direction/private void releasemoving () {ispremoving = true; 
 Iscurrmoving = true; @Override public boolean dispatchtouchevent (Motionevent event) {if (adapter!= null) switch (event.getactio 
 Nmasked ()) {Case MotionEvent.ACTION_DOWN:lastX = Event.getx (); 
  try {if (vt = null) {VT = Velocitytracker.obtain (); 
  else {vt.clear (); 
 } catch (Exception e) {e.printstacktrace (); 
 } vt.addmovement (event); 
 mevents = 0; 
 Break 
 Case MotionEvent.ACTION_POINTER_DOWN:case MotionEvent.ACTION_POINTER_UP:mEvents =-1; 
 Break 
 Case Motionevent.action_move://Cancel animation quitmove (); LOG.D ("index", "mevents =" + Mevents + ", ispremoving =" + ispremoving + ", iscurrmoving =" + iscurrmoving); 
 Vt.addmovement (event); 
 Vt.computecurrentvelocity (500); 
 Speed = Vt.getxvelocity (); 
 Movelenght = Event.getx ()-lastx; 
  if ((Movelenght > 0 | |!iscurrmoving) && ispremoving && mevents = = 0) {ispremoving = true; 
  Iscurrmoving = false; 
  if (index = = 1) {//The first page can no longer turn right, jump to the previous activity state = State_move; 
  Releasemoving (); 
  else {//non-first page prepageleft + = (int) movelenght; 
  Prevent sliding over boundary if (Prepageleft > 0) prepageleft = 0; 
  else if (Prepageleft <-mwidth) {//Boundary judgment, release action, prevent sliding back and forth so that the current page cannot slide when the previous page is sliding prepageleft =-mwidth; 
  Releasemoving (); 
  right = Mwidth + Prepageleft; 
  state = State_move; Or else if ((Movelenght < 0 | |!ispremoving) && iscurrmoving && mevents = 0) {ispremoving 
  = false; 
  Iscurrmoving = true; if (index = = Adapter.getcount ()) {//LastOne page can no longer turn left state = State_stop; 
  Releasemoving (); 
  else {currpageleft + = (int) movelenght; 
  Prevent sliding over boundary if (Currpageleft <-mwidth) Currpageleft =-mwidth; 
  else if (Currpageleft > 0) {//Boundary judgment, release action, prevent sliding back and forth causing sliding current page is previous page cannot slide currpageleft = 0; 
  Releasemoving (); 
  right = Mwidth + Currpageleft; 
  state = State_move; 
 } else mevents = 0; 
 LASTX = Event.getx (); 
 Requestlayout (); 
 Break 
 Case MotionEvent.ACTION_UP:if (Math.Abs (Speed) < Speed_shake) Speed = 0; 
 Quitmove (); 
 Mtask = new Mytimertask (Updatehandler); 
 Timer.schedule (mtask, 0, 5); 
  try {vt.clear (); 
 Vt.recycle (); 
 catch (Exception e) {e.printstacktrace (); 
 } break; 
 Default:break; 
 } super.dispatchtouchevent (event); 
 return true; * * * (non-Javadoc) draws the page shadow effect * @see Android.view.viewgroup#dispatchdraw (android.graphics.Canvas)/@Ove Rride protected void Dispatchdraw (Canvas Canvas) {Super.dispatchdraw (Canvas); 
 if (right = = 0 | | right = = mwidth) return; 
 RECTF RECTF = new RECTF (right, 0, Mwidth, mheight); 
 Paint Paint = new Paint (); 
 Paint.setantialias (TRUE); 
 LinearGradient lineargradient = new LinearGradient (right, 0, right + 0, 0xffbbbbbb, 0X00BBBBBB, Tilemode.clamp); 
 Paint.setshader (lineargradient); 
 Paint.setstyle (Style.fill); 
 Canvas.drawrect (RECTF, paint); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasure 
 Spec, Heightmeasurespec); 
 Mwidth = Getmeasuredwidth (); 
 Mheight = Getmeasuredheight (); 
 if (isinit) {//initial state, one page is hidden on the left, two pages are stacked in a prepageleft =-mwidth; 
 Currpageleft = 0; 
 Nextpageleft = 0; 
 Isinit = false; } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {if (adapter = = NULL) RE 
 Turn 
 Prepage.layout (prepageleft, 0, Prepageleft + prepage.getmeasuredwidth (), Prepage.getmeasuredheight ()); Currpage.layout (currpageleft, 0, CURrpageleft + currpage.getmeasuredwidth (), Currpage.getmeasuredheight ()); 
 Nextpage.layout (nextpageleft, 0, Nextpageleft + nextpage.getmeasuredwidth (), Nextpage.getmeasuredheight ()); 
 Invalidate (); 
 
 Class Mytimertask extends TimerTask {Handler Handler; 
 Public Mytimertask (Handler Handler) {this.handler = Handler; 
 @Override public void Run () {handler.sendmessage (Handler.obtainmessage ()); 
 } 
 
 } 
}

The comments in the code are written very much, the principle of understanding the code is easy to read. After you finish writing this layout, write a scanviewadapter inheritance pageadapter:

Package Com.jingchen.pagerdemo; 
Import Java.util.Timer; 
 
Import Java.util.TimerTask; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import android.graphics.LinearGradient; 
Import Android.graphics.Paint; 
Import Android.graphics.Paint.Style; 
Import Android.graphics.RectF; 
Import Android.graphics.Shader.TileMode; 
Import Android.os.Handler; 
Import Android.os.Message; 
Import Android.util.AttributeSet; 
Import Android.util.Log; 
Import android.view.MotionEvent; 
Import Android.view.VelocityTracker; 
Import Android.view.View; 
 
Import Android.widget.RelativeLayout; /** * @author chenjing * */public class Scanview extends Relativelayout {public static final String TAG = "Sca 
 Nview "; 
 Private Boolean isinit = true; 
 There are two pages to slide when sliding, to determine which page is sliding private Boolean ispremoving = True, iscurrmoving = true; 
 is currently the first few pages of private int index; 
 private float lastx; 
 Previous page, current page, left position of next page private int prepageleft = 0, Currpageleft = 0, nextpageleft = 0; // Three page private View prepage, Currpage, nextPage; 
 Page state private static final int state_move = 0; 
 private static final int state_stop = 1; 
 Slide the page, only the previous page and the current page can be slippery private static final int PRE = 2; 
 private static final int CURR = 3; 
 private int state = State_stop; 
 The right position of the page being slid, used to draw the shadow private float rights; 
 Fingers sliding distance private float movelenght; 
 Page wide high private int mwidth, mheight; 
 Get sliding speed private velocitytracker VT; 
 Prevent dithering private float speed_shake = 20; 
 Current sliding speed private float speed; 
 private timer timer; 
 Private Mytimertask Mtask; 
 Moving speed of the sliding animation public static final int move_speed = 10; 
 Page adapter private PageAdapter adapter; 
 
 /** * Filter Multiple TOUCH control variable * * private int mevents; 
 public void Setadapter (Scanviewadapter adapter) {removeallviews (); 
 This.adapter = adapter; 
 Prepage = Adapter.getview (); 
 AddView (prepage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); Adapter.addcontent (Prepage, index-1);
 
 Currpage = Adapter.getview (); 
 AddView (currpage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 
 Adapter.addcontent (currpage, index); 
 NextPage = Adapter.getview (); 
 AddView (nextPage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 
 Adapter.addcontent (nextPage, index + 1); /** * Slide to the left. Note the pages that can be slid are only the current page and the previous page * * @param which */private void moveLeft (int which) {switch (which) {case Pre:pre 
 Pageleft-= Move_speed; 
 if (Prepageleft <-mwidth) Prepageleft =-mwidth; 
 right = Mwidth + Prepageleft; 
 Break 
 Case Curr:currpageleft-= move_speed; 
 if (Currpageleft <-mwidth) Currpageleft =-mwidth; 
 right = Mwidth + Currpageleft; 
 Break }/** * slide to the right. Note the pages that can be slid are only the current page and the previous page * * @param which */private void moveright (int which) {switch (which) {case PRE:PR 
 Epageleft + = Move_speed; 
 if (Prepageleft > 0) prepageleft = 0; right = Mwidth + PrepageleFt 
 Break 
 Case Curr:currpageleft + = Move_speed; 
 if (Currpageleft > 0) currpageleft = 0; 
 right = Mwidth + Currpageleft; 
 Break 
 }/** * When you turn back a page, add the previous page to the left/private void Addprepage () {Removeview (nextPage); 
 AddView (NextPage,-1, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 Get the previous page content from the adapter adapter.addcontent (NextPage, index-1); 
 Exchange Order View temp = nextPage; 
 NextPage = Currpage; 
 Currpage = Prepage; 
 Prepage = temp; 
 Prepageleft =-mwidth; 
 /** * When you turn a page forward, add a page at the bottom/private void Addnextpage () {Removeview (prepage); 
 AddView (prepage, 0, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); 
 Get the next page content from the adapter adapter.addcontent (prepage, index + 1); 
 Exchange Order View temp = currpage; 
 Currpage = NextPage; 
 NextPage = Prepage; 
 Prepage = temp; 
 Currpageleft = 0; 
 Handler Updatehandler = new Handler () {@Override public void Handlemessage (msg){if (state!= state_move) return; Move the page//flip back, first determine which page is currently in the not returned status if (Prepageleft >-mwidth && speed <= 0) {//previous page is not returned in status MoveLeft (PR 
 E); 
 else if (Currpageleft < 0 && speed >= 0) {//The current page is not in the returned state MoveRight (CURR); 
 else if (Speed < 0 && index < Adapter.getcount ()) {//left turn, flip is the current page moveLeft (CURR); 
  if (Currpageleft = = (-mwidth)) {index++; 
 Turn over one page and add a page underneath to remove the top page addnextpage (); 
 } else if (Speed > 0 && index > 1) {//Turn right and flip the previous page moveright (pre); 
  if (Prepageleft = = 0) {index--; 
 Turn back a page, add a page to the top, hide in the leftmost addprepage (); 
 } if (right = = 0 | | right = = mwidth) {releasemoving (); 
 state = State_stop; 
 Quitmove (); 
 } ScanView.this.requestLayout (); 
 
 } 
 
 }; 
 Public Scanview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 Init (); 
 Public Scanview {Super (context); 
 Init (); } PUBlic Scanview (context, AttributeSet attrs) {Super (context, attrs); 
 Init (); 
 /** * Exit Animation page */public void Quitmove () {if (mtask!= null) {mtask.cancel (); 
 Mtask = null; 
 } private void Init () {index = 1; 
 Timer = new timer (); 
 Mtask = new Mytimertask (Updatehandler); 
 /** * Release action, do not restrict hand sliding direction/private void releasemoving () {ispremoving = true; 
 Iscurrmoving = true; @Override public boolean dispatchtouchevent (Motionevent event) {if (adapter!= null) switch (event.getactio 
 Nmasked ()) {Case MotionEvent.ACTION_DOWN:lastX = Event.getx (); 
  try {if (vt = null) {VT = Velocitytracker.obtain (); 
  else {vt.clear (); 
 } catch (Exception e) {e.printstacktrace (); 
 } vt.addmovement (event); 
 mevents = 0; 
 Break 
 Case MotionEvent.ACTION_POINTER_DOWN:case MotionEvent.ACTION_POINTER_UP:mEvents =-1; 
 Break 
 Case Motionevent.action_move://Cancel animation quitmove (); LOG.D ("index", "mevents =" + Mevents + ", ispremoving =" + ispremoving + ", iscurrmoving =" + iscurrmoving); 
 Vt.addmovement (event); 
 Vt.computecurrentvelocity (500); 
 Speed = Vt.getxvelocity (); 
 Movelenght = Event.getx ()-lastx; 
  if ((Movelenght > 0 | |!iscurrmoving) && ispremoving && mevents = = 0) {ispremoving = true; 
  Iscurrmoving = false; 
  if (index = = 1) {//The first page can no longer turn right, jump to the previous activity state = State_move; 
  Releasemoving (); 
  else {//non-first page prepageleft + = (int) movelenght; 
  Prevent sliding over boundary if (Prepageleft > 0) prepageleft = 0; 
  else if (Prepageleft <-mwidth) {//Boundary judgment, release action, prevent sliding back and forth so that the current page cannot slide when the previous page is sliding prepageleft =-mwidth; 
  Releasemoving (); 
  right = Mwidth + Prepageleft; 
  state = State_move; Or else if ((Movelenght < 0 | |!ispremoving) && iscurrmoving && mevents = 0) {ispremoving 
  = false; 
  Iscurrmoving = true; if (index = = Adapter.getcount ()) {//LastOne page can no longer turn left state = State_stop; 
  Releasemoving (); 
  else {currpageleft + = (int) movelenght; 
  Prevent sliding over boundary if (Currpageleft <-mwidth) Currpageleft =-mwidth; 
  else if (Currpageleft > 0) {//Boundary judgment, release action, prevent sliding back and forth causing sliding current page is previous page cannot slide currpageleft = 0; 
  Releasemoving (); 
  right = Mwidth + Currpageleft; 
  state = State_move; 
 } else mevents = 0; 
 LASTX = Event.getx (); 
 Requestlayout (); 
 Break 
 Case MotionEvent.ACTION_UP:if (Math.Abs (Speed) < Speed_shake) Speed = 0; 
 Quitmove (); 
 Mtask = new Mytimertask (Updatehandler); 
 Timer.schedule (mtask, 0, 5); 
  try {vt.clear (); 
 Vt.recycle (); 
 catch (Exception e) {e.printstacktrace (); 
 } break; 
 Default:break; 
 } super.dispatchtouchevent (event); 
 return true; * * * (non-Javadoc) draws the page shadow effect * @see Android.view.viewgroup#dispatchdraw (android.graphics.Canvas)/@Ove Rride protected void Dispatchdraw (Canvas Canvas) {Super.dispatchdraw (Canvas); 
 if (right = = 0 | | right = = mwidth) return; 
 RECTF RECTF = new RECTF (right, 0, Mwidth, mheight); 
 Paint Paint = new Paint (); 
 Paint.setantialias (TRUE); 
 LinearGradient lineargradient = new LinearGradient (right, 0, right + 0, 0xffbbbbbb, 0X00BBBBBB, Tilemode.clamp); 
 Paint.setshader (lineargradient); 
 Paint.setstyle (Style.fill); 
 Canvas.drawrect (RECTF, paint); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasure 
 Spec, Heightmeasurespec); 
 Mwidth = Getmeasuredwidth (); 
 Mheight = Getmeasuredheight (); 
 if (isinit) {//initial state, one page is hidden on the left, two pages are stacked in a prepageleft =-mwidth; 
 Currpageleft = 0; 
 Nextpageleft = 0; 
 Isinit = false; } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {if (adapter = = NULL) RE 
 Turn 
 Prepage.layout (prepageleft, 0, Prepageleft + prepage.getmeasuredwidth (), Prepage.getmeasuredheight ()); Currpage.layout (currpageleft, 0, CURrpageleft + currpage.getmeasuredwidth (), Currpage.getmeasuredheight ()); 
 Nextpage.layout (nextpageleft, 0, Nextpageleft + nextpage.getmeasuredwidth (), Nextpage.getmeasuredheight ()); 
 Invalidate (); 
 
 Class Mytimertask extends TimerTask {Handler Handler; 
 Public Mytimertask (Handler Handler) {this.handler = Handler; 
 @Override public void Run () {handler.sendmessage (Handler.obtainmessage ()); 
 } 
 
 } 
}

This is just the adapter in my demo, and I can write adapter with more content. Addcontent's parameter view is the view that is returned in GetView, so that you can set the content according to the inflate layout, GetView return the layout page_layout.xml as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 android:layout_width= "Match_ Parent " 
 android:layout_height=" match_parent " 
 android:background=" @drawable/cover "> 
 
 <textview 
 android:id= "@+id/content" 
 android:layout_width= "wrap_content" 
 android:layout_height= "Wrap_content" " 
 android:layout_centerhorizontal=" true " 
 android:layout_margintop=" 60DP " 
 android:padding=" 10DP " 
 android:textcolor= "#000000" 
 android:textsize= "22sp"/> 
 
 <textview 
 android:id= "@+id/ Index " 
 android:layout_width=" wrap_content " 
 android:layout_height=" wrap_content " 
 android:layout_ Alignparentbottom= "true" 
 android:layout_centerhorizontal= "true" 
 android:layout_marginbottom= "60DP" 
 android:textcolor= "#000000" 
 android:textsize= "30sp"/> 
 
</RelativeLayout> 

Contains only two TextView, so in adapter you can find the two TextView based on the ID and then set the content for it.
OK, Mainactivity's layout is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 android:layout_width= "Match_ Parent " 
 android:layout_height=" match_parent " 
 android:background=" @drawable/cover "> 
 
 <textview 
 android:id= "@+id/content" 
 android:layout_width= "wrap_content" 
 android:layout_height= "Wrap_content" " 
 android:layout_centerhorizontal=" true " 
 android:layout_margintop=" 60DP " 
 android:padding=" 10DP " 
 android:textcolor= "#000000" 
 android:textsize= "22sp"/> 
 
 <textview 
 android:id= "@+id/ Index " 
 android:layout_width=" wrap_content " 
 android:layout_height=" wrap_content " 
 android:layout_ Alignparentbottom= "true" 
 android:layout_centerhorizontal= "true" 
 android:layout_marginbottom= "60DP" 
 android:textcolor= "#000000" 
 android:textsize= "30sp"/> 
 
</RelativeLayout> 

It's simple, it contains only scanview.
Code for Mainactivity:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 android:layout_width= "Match_ Parent " 
 android:layout_height=" match_parent " 
 android:background=" @drawable/cover "> 
 
 <textview 
 android:id= "@+id/content" 
 android:layout_width= "wrap_content" 
 android:layout_height= "Wrap_content" " 
 android:layout_centerhorizontal=" true " 
 android:layout_margintop=" 60DP " 
 android:padding=" 10DP " 
 android:textcolor= "#000000" 
 android:textsize= "22sp"/> 
 
 <textview 
 android:id= "@+id/ Index " 
 android:layout_width=" wrap_content " 
 android:layout_height=" wrap_content " 
 android:layout_ Alignparentbottom= "true" 
 android:layout_centerhorizontal= "true" 
 android:layout_marginbottom= "60DP" 
 android:textcolor= "#000000" 
 android:textsize= "30sp"/> 
 
</RelativeLayout> 

Set the adapter for Scanview.
OK, look at the translation of the translated page is done.

I hope this article will help you learn about Android software programming.

Related Article

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.