广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义ViewGroup横向布局(1)
  • 711
分享到

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

布局Android 2022-06-06 05:06:49 711人浏览 泡泡鱼
摘要

最近学习自定义viewgroup,我的目标是做一个可以很想滚动的listview,使用adapter填充数据,并且使用adapter.notifyDataSetChanged(

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

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);
  }
}
您可能感兴趣的文章:Android动画效果之自定义ViewGroup添加布局动画(五)Android自定义ViewGroup之实现FlowLayout流式布局Android编程重写ViewGroup实现卡片布局的方法Android App开发中自定义View和ViewGroup的实例教程Android自定义ViewGroup的实现方法Android自定义ViewGroup打造各种风格的SlidingMenu从源码解析Android中View的容器ViewGroupAndroid自定义ViewGroup实现标签浮动效果Android应用开发中自定义ViewGroup的究极攻略Android自定义ViewGroup实现带箭头的圆角矩形菜单


--结束END--

本文标题: Android自定义ViewGroup横向布局(1)

本文链接: https://www.lsjlt.com/news/22863.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作