国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義ViewGroup實現可滾動的橫向布局(2)

2019-12-12 04:16:36
字體:
來源:轉載
供稿:網友

上一篇文章自定義viewgroup(1)地址://m.survivalescaperooms.com/article/100608.htm

這里直接代碼:

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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东安县| 青浦区| 七台河市| 通渭县| 安岳县| 巴中市| 洛浦县| 淮安市| 浦北县| 宝鸡市| 达日县| 宁陕县| 兴仁县| 平谷区| 乌什县| 宜都市| 肥东县| 交口县| 砚山县| 临泉县| 阿拉善盟| 蓬溪县| 乡宁县| 亚东县| 阿拉尔市| 合肥市| 静安区| 高青县| 嘉禾县| 株洲县| 清新县| 桐城市| 扬州市| 大洼县| 扶余县| 咸宁市| 南丰县| 麻城市| 芜湖县| 漠河县| 乌拉特前旗|