Android如何自定義ViewGroup實現堆疊頭像的點贊Layout

這篇文章給大家分享的是有關Android如何自定義ViewGroup實現堆疊頭像的點贊Layout的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

專業領域包括成都做網站、網站建設、外貿營銷網站建設成都商城網站開發、微信營銷、系統平臺開發, 與其他網站設計及系統開發公司不同,創新互聯的整合解決方案結合了幫做網絡品牌建設經驗和互聯網整合營銷的理念,并將策略和執行緊密結合,為客戶提供全網互聯網整合方案。

實現

自定義屬性

屬性名說明默認值
vertivalSpace行距4dp
pileWidth重疊寬度10dp

onMeasure方法,每行的寬度不再是child的寬度和了,而是要減掉重疊部分的寬度和

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

    //AT_MOST
    int width = 0;
    int height = 0;
    int rawWidth = 0;//當前行總寬度
    int rawHeight = 0;// 當前行高

    int rowIndex = 0;//當前行位置
    int count = getChildCount();
    for (int i = 0; i < count; i++) {
      View child = getChildAt(i);
      if(child.getVisibility() == GONE){
        if(i == count - 1){
          //最后一個child
          height += rawHeight;
          width = Math.max(width, rawWidth);
        }
        continue;
      }

      //這里調用measureChildWithMargins 而不是measureChild
      measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
      MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

      int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
      int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
      if(rawWidth + childWidth - (rowIndex > 0 ? pileWidth : 0)> widthSpecSize - getPaddingLeft() - getPaddingRight()){
        //換行
        width = Math.max(width, rawWidth);
        rawWidth = childWidth;
        height += rawHeight + vertivalSpace;
        rawHeight = childHeight;
        rowIndex = 0;
      } else {
        rawWidth += childWidth;
        if(rowIndex > 0){
          rawWidth -= pileWidth;
        }
        rawHeight = Math.max(rawHeight, childHeight);
      }

      if(i == count - 1){
        width = Math.max(rawWidth, width);
        height += rawHeight;
      }

      rowIndex++;
    }

    setMeasuredDimension(
        widthSpecMode == MeasureSpec.EXACTLY ? widthSpecSize : width + getPaddingLeft() + getPaddingRight(),
        heightSpecMode == MeasureSpec.EXACTLY ? heightSpecSize : height + getPaddingTop() + getPaddingBottom()
    );
  }

onLayout 每一行,第一個正常放,之后的重疊放

@Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int viewWidth = r - l;
    int leftOffset = getPaddingLeft();
    int topOffset = getPaddingTop();
    int rowMaxHeight = 0;
    int rowIndex = 0;//當前行位置
    View childView;
    for( int w = 0, count = getChildCount(); w < count; w++ ){
      childView = getChildAt(w);
      if(childView.getVisibility() == GONE) continue;

      MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
      // 如果加上當前子View的寬度后超過了ViewGroup的寬度,就換行
      int occupyWidth = lp.leftMargin + childView.getMeasuredWidth() + lp.rightMargin;
      if(leftOffset + occupyWidth + getPaddingRight() > viewWidth){
        leftOffset = getPaddingLeft(); // 回到最左邊
        topOffset += rowMaxHeight + vertivalSpace; // 換行
        rowMaxHeight = 0;

        rowIndex = 0;
      }

      int left = leftOffset + lp.leftMargin;
      int top = topOffset + lp.topMargin;
      int right = leftOffset+ lp.leftMargin + childView.getMeasuredWidth();
      int bottom = topOffset + lp.topMargin + childView.getMeasuredHeight();
      childView.layout(left, top, right, bottom);

      // 橫向偏移
      leftOffset += occupyWidth;
      // 試圖更新本行最高View的高度
      int occupyHeight = lp.topMargin + childView.getMeasuredHeight() + lp.bottomMargin;
      if(rowIndex != count - 1){
        leftOffset -= pileWidth;//這里控制重疊位置
      }
      rowMaxHeight = Math.max(rowMaxHeight, occupyHeight);
      rowIndex++;
    }
  }

效果圖

Android如何自定義ViewGroup實現堆疊頭像的點贊Layout

因為這個一般只會顯示一行,所以暫時沒有通過setAdapter方式去設置數據源。

感謝各位的閱讀!關于“Android如何自定義ViewGroup實現堆疊頭像的點贊Layout”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章名稱:Android如何自定義ViewGroup實現堆疊頭像的點贊Layout
地址分享:http://m.kartarina.com/article48/jecdep.html

成都網站建設公司_創新互聯,為您提供網站排名網站營銷動態網站商城網站響應式網站網站策劃

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

商城網站建設
主站蜘蛛池模板: 无码的免费不卡毛片视频| 一本大道在线无码一区| 亚洲中文字幕无码爆乳| 高清无码午夜福利在线观看 | 国产精品无码专区在线观看| 亚洲AV永久无码精品水牛影视| 无码射肉在线播放视频| 精品无码一区二区三区在线| 亚洲AV无码一区二区三区系列 | 中文无码热在线视频| 日产无码1区2区在线观看| 久久久久久国产精品免费无码| 国产成人无码18禁午夜福利p| 亚洲av永久无码| 无码人妻丰满熟妇区免费| 亚洲AV区无码字幕中文色| 国产成人午夜无码电影在线观看| 久久久久无码精品国产h动漫| 亚洲AV无码之国产精品| 人妻丰满熟妇AV无码区乱| 免费无码又爽又高潮视频| 免费无遮挡无码视频在线观看| 未满十八18禁止免费无码网站| 丰满日韩放荡少妇无码视频| 亚洲av无码一区二区三区网站| 国内精品久久人妻无码不卡| 免费VA在线观看无码| 亚洲国产成人精品无码区二本| 亚洲成AV人在线观看天堂无码| 亚洲精品一级无码中文字幕| 在线播放无码后入内射少妇| 国产免费午夜a无码v视频| 日韩av无码成人无码免费| 黄色成人网站免费无码av| 无码中文在线二区免费| 无码av专区丝袜专区| 国产精品无码av片在线观看播| 无码一区二区三区中文字幕| 麻豆亚洲AV成人无码久久精品 | 无码日韩人妻精品久久蜜桃| 一本加勒比hezyo无码专区|