iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android开发实现自定义水平滚动的容器示例
  • 444
分享到

Android开发实现自定义水平滚动的容器示例

android容器roi 2023-05-30 19:05:17 444人浏览 八月长安
摘要

本文实例讲述了Android开发实现自定义水平滚动的容器。分享给大家供大家参考,具体如下:public class HorizontalScrollView extends ViewGroup { //手势 private Gestur

本文实例讲述了Android开发实现自定义水平滚动的容器。分享给大家供大家参考,具体如下:

public class HorizontalScrollView extends ViewGroup {  //手势  private GestureDetector mGestureDetector;  private HorizontalScroller mScroller;  private int curID;  //快速滑动  private boolean isFlying;  //--回调函数-------------------------------------  private OnChangeListener mListener;  public void setOnChangeListener(OnChangeListener listener) {    if (listener != null) {      mListener = listener;    }  }  public interface OnChangeListener{    void move2dest(int curID);  }  public HorizontalScrollView(Context context) {    this(context, null);  }  public HorizontalScrollView(Context context, AttributeSet attrs) {    this(context, attrs, 0);  }  public HorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    mScroller = new HorizontalScroller();    isFlying = false;    initGesture(context);  }  @Override  protected void onLayout(boolean changed, int l, int t, int r, int b) {    // 模向移动,    for (int i = 0; i < getChildCount(); i++) {      View view = getChildAt(i);      //给水平方向的每个view定位      view.layout(i * getWidth(), 0, getWidth() + i * getWidth(), getHeight());    }  }  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    for (int i = 0; i < getChildCount(); i++) {      View view = getChildAt(i);      view.measure(widthMeasureSpec, heightMeasureSpec);    }    super.onMeasure(widthMeasureSpec, heightMeasureSpec);  }  @Override  public boolean onTouchEvent(MotionEvent event) {    mGestureDetector.onTouchEvent(event);    switch (event.getAction()) {    case MotionEvent.ACTION_DOWN:      if (!isFlying) {        move2dest();      }      isFlying = false;      break;    case MotionEvent.ACTION_MOVE:      break;    case MotionEvent.ACTION_UP:      break;    default:      break;    }    return true;  }  public void move2dest() {    //    int destID = (getScrollX() + getWidth() / 2) / getWidth();    move2dest(destID);  }  public void move2dest(int destID) {    curID = destID;    if (destID > getChildCount() - 1) {      destID = getChildCount() - 1;    }    if (mListener != null) {      mListener.move2dest(curID);    }    int distance = (int) (destID * getWidth() - getScrollX());    // scrollBy(distance, 0);    mScroller.startScroll(getScrollX(), getScrollY(), distance, 0);    invalidate();  }    @Override  public void computeScroll() {    // 如果存在偏移,就不断刷新    if (mScroller.computeScrollOffset()) {      scrollTo(mScroller.getCurrX(), 0);      invalidate();    }    super.computeScroll();  }  private void initGesture(Context context) {    mGestureDetector = new GestureDetector(context, new OnGestureListener() {      @Override      public boolean onSingleTapUp(MotionEvent e) {        return false;      }      @Override      public void onShowPress(MotionEvent e) {      }      @Override      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {        scrollBy((int) distanceX, 0);        return false;      }      @Override      public void onLongPress(MotionEvent e) {      }      @Override            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {        isFlying = true;        if (curID > 0 && velocityX > 0) {// 表示向左移          move2dest(curID - 1);        } else if (curID < getChildCount() && velocityX < 0) {          move2dest(curID + 1);// 向右        } else {          move2dest();// 移到原位        }        return false;      }      @Override      public boolean onDown(MotionEvent e) {        return false;      }    });  }}

--结束END--

本文标题: Android开发实现自定义水平滚动的容器示例

本文链接: https://www.lsjlt.com/news/220656.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作