Android自訂ViewGroup橫向布局(1)_Android

來源:互聯網
上載者:User

最近學習自訂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);  }}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.