欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义ViewGroup实现可滚动的横向布局(2)

程序员文章站 2024-02-24 13:35:34
上一篇文章自定义viewgroup(1)地址: 这里直接代码: package com.example.libingyuan.horizontallistvie...

上一篇文章自定义viewgroup(1)地址:

这里直接代码:

package com.example.libingyuan.horizontallistview.scrollviewgroup;

import android.content.context;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.widget.scroller;

/**
 * 自定义viewgroup
 * 在横向布局的基础上,增加啦滚动效果,但是没有边界限制
 */
public class scrollviewgroup extends viewgroup {
  private scroller mscroller;
  private float mlastmotionx = 0;

  public scrollviewgroup(context context) {
    this(context, null);
  }

  public scrollviewgroup(context context, attributeset attrs) {
    this(context, attrs, 0);
  }

  public scrollviewgroup(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    init(context);
  }

  private void init(context context) {
    mscroller = new scroller(context);
  }

  @override
  public void computescroll() {
    if (mscroller.computescrolloffset()) {
      scrollto(mscroller.getcurrx(), mscroller.getcurry());
      postinvalidate();
    }
  }


  @override
  public boolean ontouchevent(motionevent event) {
    // todo auto-generated method stub
    int action = event.getaction();
    float x = event.getx();
    switch (action) {
      case motionevent.action_down:
        if (!mscroller.isfinished()) {
          mscroller.abortanimation();
        }
        mlastmotionx = event.getx();
        break;
      case motionevent.action_move:
        float delt = mlastmotionx - x;
        mlastmotionx = x;
        scrollby((int) delt, 0);
        break;
      case motionevent.action_up:
        invalidate();
        break;

      default:
        break;
    }

    return true;
  }

  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    //重新设置宽高
    this.setmeasureddimension(measurewidth(widthmeasurespec, heightmeasurespec), measureheight(widthmeasurespec, heightmeasurespec));
  }

  /**
   * 测量宽度
   */
  private int measurewidth(int widthmeasurespec, int heightmeasurespec) {
    // 宽度
    int sizewidth = measurespec.getsize(widthmeasurespec);
    int modewidth = measurespec.getmode(widthmeasurespec);
    //父控件的宽(wrap_content)
    int width = 0;
    int childcount = getchildcount();

    //重新测量子view的宽度,以及最大高度
    for (int i = 0; i < childcount; i++) {
      view child = getchildat(i);
      measurechild(child, widthmeasurespec, heightmeasurespec);
      marginlayoutparams lp = (marginlayoutparams) child.getlayoutparams();
      int childwidth = child.getmeasuredwidth() + lp.leftmargin + lp.rightmargin;
      width += childwidth;
    }
    return modewidth == measurespec.exactly ? sizewidth : width;
  }

  /**
   * 测量高度
   */
  private int measureheight(int widthmeasurespec, int heightmeasurespec) {
    //高度
    int sizeheight = measurespec.getsize(heightmeasurespec);
    int modeheight = measurespec.getmode(heightmeasurespec);
    //父控件的高(wrap_content)
    int height = 0;
    int childcount = getchildcount();

    //重新测量子view的宽度,以及最大高度
    for (int i = 0; i < childcount; i++) {
      view child = getchildat(i);
      measurechild(child, widthmeasurespec, heightmeasurespec);
      marginlayoutparams lp = (marginlayoutparams) child.getlayoutparams();
      int childheight = child.getmeasuredheight() + lp.topmargin + lp.bottommargin;
      height += childheight;
    }
    height = height / childcount;
    return modeheight == measurespec.exactly ? sizeheight : height;
  }

  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    int childleft = 0;
    int childwidth;
    int height = getheight();
    int childcount = getchildcount();
    for (int i = 0; i < childcount; i++) {
      view child = getchildat(i);
      marginlayoutparams lp = (marginlayoutparams) child.getlayoutparams();
      childwidth = child.getmeasuredwidth() + lp.leftmargin + lp.rightmargin;
      child.layout(childleft, 0, childleft + childwidth, height);
      childleft += childwidth;
    }
  }

  @override
  public layoutparams generatelayoutparams(attributeset attrs) {
    return new marginlayoutparams(getcontext(), attrs);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: pyqt4 menu bar

下一篇: Git 设置别名