当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义ViewGroup横向布局(1)

Android自定义ViewGroup横向布局(1)

2019年07月24日  | 移动技术网移动技术  | 我要评论
最近学习自定义viewgroup,我的目标是做一个可以很想滚动的listview,使用adapter填充数据,并且使用adapter.notifydatasetchange

最近学习自定义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);
  }
}

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网