Put the code for this custom view first,
Package com.example.viewpagervertical;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.VelocityTracker;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.animation.Interpolator;
Import Android.widget.Scroller;
Import Android.widget.Toast;
/**
* To achieve the effect of sliding up and down, mainly using the screen coordinate system and scroll bar
* @author Administrator
*
*/
public class Verticalpager extends viewgroup{
private Scroller Mscroller; scroll bar
private Context mcontext;
Private final static int rate = 5; Rate Standard
private final static int DISTANCE = 300;//the distance to scroll
Private Velocitytracker mvelocitytracker;//This class allows you to calculate the speed
Public Verticalpager (context context, AttributeSet Attrs) {
Super (context, attrs);
This.mcontext=context;
mscroller=new Scroller (context);
}
@Override
protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
int totalheight=0;
int Count=getchildcount ();
for (int i=0;i<count;i++) {
View Childview=getchildat (i);
childview.layout (L, Totalheight, R, Totalheight+b);
totalheight+=b;
}
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int width=measurespec.getsize (WIDTHMEASURESPEC);
int height=measurespec.getsize (HEIGHTMEASURESPEC);
int Count=getchildcount ();
for (int i=0;i<count;i++) {
Getchildat (i). Measure (width, height);
}
setmeasureddimension (width, height);
}
private int mlastmotiony;
@Override
Public boolean ontouchevent (Motionevent event) {
if (mvelocitytracker==null) {
Mvelocitytracker=velocitytracker.obtain ();
}
mvelocitytracker.addmovement (event);
int action=event.getaction ();
float y=event.gety ();
Switch (action) {
Case Motionevent.action_down:
if (!mscroller.isfinished ()) {
mscroller.abortanimation ();
}
mlastmotiony= (int) y;
log.d ("Montion", "" +getscrolly ());
break;
Case Motionevent.action_move:
int deltay= (int) (MLASTMOTIONY-Y);
Scrollby (0,deltay);
invalidate ();
mlastmotiony= (int) y;
break;
Case MOTIONEVENT.ACTION_UP:
if (mvelocitytracker!=null) {
mvelocitytracker.recycle ();
Mvelocitytracker=null;
// }
mvelocitytracker.computecurrentvelocity (1, 1000); Unit is 1 description, one second per pixel, maximum value is 1000
Float vy = mvelocitytracker.getyvelocity (); VY represents the y-axis direction of the rate
log.i ("Test", "Velocitytraker:" +mvelocitytracker.getyvelocity ());
if (getscrolly () <0) {
mscroller.startscroll (0,-distance, 0, DISTANCE);
}else if (getscrolly () > (GetHeight () * (Getchildcount ()-1)) {
View Lastview=getchildat (Getchildcount ()-1);
Mscroller.startscroll (0,lastview.gettop () +distance, 0,-distance);
}else{
int position=getscrolly ()/getheight ();
View positionview = null;
if (vy<-rate) {//Slide
Positionview=getchildat (position+1);
mscroller.startscroll (0, Positionview.gettop ()-distance, 0, +distance);
}else if (vy>rate) {//Slide up
Positionview=getchildat (position);
mscroller.startscroll (0, Positionview.gettop ()-distance, 0, +distance);
}else {
int mod=getscrolly ()%getheight ();
if (Mod>getheight ()/2) {
Positionview=getchildat (position+1);
mscroller.startscroll (0, Positionview.gettop ()-distance, 0, +distance);
}else{
Positionview=getchildat (position);
mscroller.startscroll (0, Positionview.gettop () +distance, 0,-distance);
}
}
}
invalidate ();
break;
}
return true; Returns true to indicate that the event was consumed by this view
}
@Override
public void Computescroll () {
Super.computescroll ();
if (Mscroller.computescrolloffset ()) {
scrollTo (0, Mscroller.getcurry ());
}
}
}
Then the layout file Activity_main.xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<com.example.viewpagervertical.verticalpager
Android:id= "@+id/vertypager1"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<textview
android:background= "@drawable/iv_guide1"
Android:id= "@+id/textview1"
Android:layout_width= "Match_parent"
android:layout_height= "100DP"
android:text= "TextView1111"/>
<textview
android:background= "@drawable/iv_guide2"
Android:id= "@+id/textview2"
Android:layout_width= "Fill_parent"
android:layout_height= "100DP"
android:text= "TextView2222"/>
<textview
android:background= "@drawable/iv_guide3"
Android:id= "@+id/textview3"
Android:layout_width= "Fill_parent"
android:layout_height= "100DP"
android:text= "TextView2222"/>
<textview
android:background= "@drawable/iv_guide4"
Android:id= "@+id/textview4"
Android:layout_width= "Fill_parent"
android:layout_height= "100DP"
android:text= "TextView2222"/>
</com.example.viewpagervertical.VerticalPager>
</LinearLayout>
Mainactivity.java do not move, the eclipse is created by the line.
Android up and down sliding view implementation