Android自定义ViewPager实例,android 仿viewpager

4
回复
895
查看
[复制链接]

501

主题

1209

帖子

2041

安币

手工艺人

发表于 2018-1-22 14:49:58 | 显示全部楼层 |阅读模式

            

        本文实例讲述了android自定义viewpager的方法。分享给大家供大家参考,具体如下:

[Java] 查看源文件 复制代码
package com.rong.activity;
import android.content.context;
import android.graphics.color;
import android.util.attributeset;
import android.view.motionevent;
import android.view.velocitytracker;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.scroller;
public class customviewpager extends viewgroup {
  private context mcontext;
  private int screenwidth;
  private int screenhight;
  private int lastmovex = 0;
  private velocitytracker velocitytracker;
  private int max_velocity=600;
  private int curscreen=0;
  private scroller scroller;
  public customviewpager(context context) {
    super(context);
    mcontext = context;
    initview();
  }
  public customviewpager(context context, attributeset attrs) {
    super(context, attrs);
    mcontext = context;
    initview();
  }
  public customviewpager(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
    mcontext = context;
    initview();
  }
  @override
  public boolean ontouchevent(motionevent event) {
    if (velocitytracker == null) {
      velocitytracker = velocitytracker.obtain();
    }
    velocitytracker.addmovement(event);
    int x = (int) event.getx();
    switch (event.getaction()) {
    case motionevent.action_down:
      lastmovex = x;
      break;
    case motionevent.action_move:
      int dis = lastmovex - x;
      scrollby(dis, 0);
      lastmovex = x;
      break;
    case motionevent.action_up:
      velocitytracker.computecurrentvelocity(1000);
      int velocityx=(int)velocitytracker.getxvelocity();
      if(velocityx>max_velocity&&curscreen>0){
        jump2screen(curscreen-1);
      }else if(velocityx<-max_velocity&&curscreen<getchildcount()-1){
        jump2screen(curscreen+1);
      }else{
        int screen=(getscrollx()+screenwidth/2)/screenwidth;
        jump2screen(screen);
      }
      if (velocitytracker != null) {
        velocitytracker.recycle();
        velocitytracker = null;
      }
      break;
    }
    return true;
  }
  /**
   * 跳转到指定screen
   * @param screen
   */
  public void jump2screen(int screen){
    curscreen=screen;
    if(curscreen>getchildcount()-1){
      curscreen=getchildcount()-1;
    }
    int dis=curscreen*screenwidth-getscrollx();
    scroller.startscroll(getscrollx(), 0, dis, 0);
    invalidate();
  }
  @override
  public void computescroll() {
    if(scroller.computescrolloffset()){
      scrollto(scroller.getcurrx(), 0);
      postinvalidate();
    }
  }
  public void initview() {
    scroller=new scroller(mcontext);
    linearlayout layout1 = new linearlayout(getcontext());
    layout1.setbackgroundcolor(color.red);
    addview(layout1);
    linearlayout layout2 = new linearlayout(getcontext());
    layout2.setbackgroundcolor(color.green);
    addview(layout2);
    linearlayout layout3 = new linearlayout(getcontext());
    layout3.setbackgroundcolor(color.blue);
    addview(layout3);
  }
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);
    screenwidth = measurespec.getsize(widthmeasurespec);
    screenhight = measurespec.getsize(heightmeasurespec);
    setmeasureddimension(screenwidth, screenhight);
    for (int i = 0; i < getchildcount(); i++) {
      view view = getchildat(i);
      view.measure(screenwidth, screenhight);
    }
  }
  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    int leftwidth = 0;
    for (int i = 0; i < getchildcount(); i++) {
      view view = getchildat(i);
      view.layout(leftwidth, 0, leftwidth + screenwidth, screenhight);
      leftwidth = leftwidth + screenwidth;
    }
  }
}

        更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》



        

14

主题

9459

帖子

4727

安币

码皇(巴士元老)

Rank: 8Rank: 8

发表于 2018-1-23 08:34:19 | 显示全部楼层
楼主威武,以后多发干货,多办活动~!

0

主题

9510

帖子

1146

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-23 23:49:42 | 显示全部楼层
每次我都积极回帖的,想要安币~

1

主题

9429

帖子

2550

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-24 17:23:28 | 显示全部楼层
感谢分享,楼主V5~

0

主题

9302

帖子

2901

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-25 13:44:10 | 显示全部楼层
支持楼主,支持安卓巴士!

0

主题

9184

帖子

2379

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-25 13:51:24 | 显示全部楼层
感谢大神~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )