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

首頁 > 系統 > Android > 正文

Android自定義ViewGroup橫向布局(1)

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

最近學習自定義viewgroup,我的目標是做一個可以很想滾動的listview,使用adapter填充數據,并且使用adapter.notifyDataSetChanged()更新數據。

不過一口吃不成一個胖子(我吃成這樣可是好幾年的積累下來的~~~~),我們一步一步來,這篇筆記首先寫一個橫向的布局。

代碼:

package com.example.libingyuan.horizontallistview.ScrollViewGroup;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * 自定義ViewGroup * 很簡單的橫向布局,把所有的子View都橫著排列起來,不可滾動 */public class ScrollViewGroup extends ViewGroup{  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);  }  @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;    //子View的個數    int childCount = getChildCount();    //重新測量子view的寬度,以及最大高度    for (int i = 0; i < childCount; i++) {      //獲取子View      View child = getChildAt(i);      //測量子View,無論什么模式,這句必須有否則界面不顯示子View(一片空白)      measureChild(child, widthMeasureSpec, heightMeasureSpec);      //得到子View的邊距      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;    //子View的個數    int childCount = getChildCount();    //重新測量子view的寬度,以及最大高度    for (int i = 0; i < childCount; i++) {      //得到子View      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;//子View左邊的距離    int childWidth;//子View的寬度    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);  }}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山阳县| 沁源县| 团风县| 博乐市| 珲春市| 衡东县| 江都市| 公主岭市| 沧州市| 察隅县| 凤山市| 射洪县| 本溪市| 旺苍县| 眉山市| 蕉岭县| 静海县| 六安市| 郯城县| 堆龙德庆县| 许昌市| 南江县| 广元市| 临西县| 嘉黎县| 九寨沟县| 比如县| 时尚| 福鼎市| 周至县| 郓城县| 南澳县| 大城县| 聂拉木县| 廊坊市| 阿克陶县| 南涧| 鲁山县| 阳城县| 武穴市| 桐庐县|