Android开发中自定义水平滚动容器的实现技巧

来源:爱站网时间:2021-03-15编辑:网友分享
Android开发中,很多功能都需要程序员自定义的开发,但是有很多字段都是无法显示的,本文是爱站技术频道小编为大家带来的Android开发中自定义水平滚动容器的实现技巧,一起进入下文学习吧!

Android开发中,很多功能都需要程序员自定义的开发,但是有很多字段都是无法显示的,本文是爱站技术频道小编为大家带来的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() - 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();
  }
  /**
   * 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 
/**
 * 位移计算工具类
 *
 * @author chenlin
 *
 */
public class HorizontalScroller {
  private int startX;
  private int startY;
  private int distanceX;
  private int distanceY;
  private int currentX;
  private int currentY;
  private long startTime;
  private long duration = 1000L;
  private boolean isFinish;
  /**
   *
   * @param scrollX
   *      x坐标
   * @param scrollY
   *      y坐标
   * @param distanceX
   *      X方向移动的距离
   * @param distanceY
   *      y方向移动的距离
   */
  public void startScroll(int scrollX, int scrollY, int distanceX, int distanceY) {
    startX = scrollX;
    startY = scrollY;
    this.distanceX = distanceX;
    this.distanceY = distanceY;
    isFinish = false;
    startTime = SystemClock.uptimeMillis();
  }
  /**
   * 计算偏移量,
   *
   * @return true 还在移动 false:移动已经停止
   */
  public boolean computeScrollOffset() {
    if (isFinish) {
      return false;
    }
    long timePassed = SystemClock.uptimeMillis() - startTime;
    if (timePassed = duration) {
      currentX = startX + distanceX;
      currentY = startY + distanceY;
      isFinish = true;
    }
    return true;
  }
  public int getCurrX() {
    return currentX;
  }
  public void setCurrentX(int currentX) {
    this.currentX = currentX;
  }
  public int getCurrentY() {
    return currentY;
  }
  public void setCurrentY(int currentY) {
    this.currentY = currentY;
  }
}

使用方法

public class ScrollActivity extends Activity implements OnCheckedChangeListener, OnChangeListener {
  private int[] ids = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6 };
  private HorizontalScrollView mView;
  private LinearLayout mLayout;
  private RadioGroup mGroup;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_myscrollview);
    init();
  }
  private void init() {
    mLayout = (LinearLayout) findViewById(R.id.body_layout);
    mGroup = (RadioGroup) findViewById(R.id.radiogroup);
    mView = new HorizontalScrollView(this);
    for (int i = 0; i 

布局文件

以上就是爱站技术频道小编给大家讲解的Android开发中自定义水平滚动容器的实现技巧,想要顺利的加入这个行业,就要不断的进步哦。

上一篇:Android开发中输入对话框的简单操作

下一篇:Android开发中新闻加载页面的实现

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载