iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义ViewGroup实现可滚动的横向布局(2)
  • 480
分享到

Android自定义ViewGroup实现可滚动的横向布局(2)

布局Android 2022-06-06 05:06:49 480人浏览 独家记忆
摘要

上一篇文章自定义viewgroup(1)地址://www.jb51.net/article/100608.htm 这里直接代码: package com.example.li

上一篇文章自定义viewgroup(1)地址://www.jb51.net/article/100608.htm

这里直接代码:


package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import Android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

public class ScrollViewGroup extends ViewGroup {
  private Scroller mScroller;
  private float mLastMotionX = 0;
  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);
    init(context);
  }
  private void init(Context context) {
    mScroller = new Scroller(context);
  }
  @Override
  public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
      scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
      postInvalidate();
    }
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int action = event.getAction();
    float x = event.getX();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
        mLastMotionX = event.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        float delt = mLastMotionX - x;
        mLastMotionX = x;
        scrollBy((int) delt, 0);
        break;
      case MotionEvent.ACTION_UP:
        invalidate();
        break;
      default:
        break;
    }
    return true;
  }
  @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;
    int childCount = getChildCount();
    //重新测量子view的宽度,以及最大高度
    for (int i = 0; i < childCount; i++) {
      View child = getChildAt(i);
      measureChild(child, widthMeasureSpec, heightMeasureSpec);
      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;
    int childCount = getChildCount();
    //重新测量子view的宽度,以及最大高度
    for (int i = 0; i < childCount; i++) {
      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;
    int childWidth;
    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实现标签浮动效果Android App开发中自定义View和ViewGroup的实例教程Android自定义ViewGroup之实现FlowLayout流式布局Android自定义ViewGroup实现堆叠头像的点赞LayoutAndroid应用开发中自定义ViewGroup的究极攻略Android动画效果之自定义ViewGroup添加布局动画(五)Android自定义ViewGroup实现受边界限制的滚动操作(3)Android自定义ViewGroup的实现方法Android进阶教程之ViewGroup自定义布局


--结束END--

本文标题: Android自定义ViewGroup实现可滚动的横向布局(2)

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

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

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

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

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

  • 微信公众号

  • 商务合作