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

 更新时间:2016年12月20日 14:07:20   作者:封魔之殇  
这篇文章主要介绍了Android自定义ViewGroup实现可滚动的横向布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

上一篇文章自定义viewgroup(1)地址:https://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;

/**
 * 自定义ViewGroup
 * 在横向布局的基础上,增加啦滚动效果,但是没有边界限制
 */
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 Retrofit使用详细教程

    Android Retrofit使用详细教程

    Retrofit是Android用来接口请求的网络框架,内部是基于OkHttp实现的,retrofit负责接口请求的封装,retrofit可以直接将接口数据解析为Bean类、List集合等,直接简化了中间繁琐的数据解析过程,这篇文章主要介绍了Android Retrofit使用详情,需要的朋友可以参考下
    2024-03-03
  • Android基于widget组件实现物体移动/控件拖动功能示例

    Android基于widget组件实现物体移动/控件拖动功能示例

    这篇文章主要介绍了Android基于widget组件实现物体移动/控件拖动功能,结合实例形式分析了widget组件在桌面应用中的事件响应与属性动态操作相关实现技巧,需要的朋友可以参考下
    2016-10-10
  • 基于Android Flutter编写贪吃蛇游戏

    基于Android Flutter编写贪吃蛇游戏

    贪吃蛇是一款足够经典的游戏。本文将利用Android中的Flutter编写这一经典的小游戏,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-03-03
  • Android实现长按圆环动画View效果的思路代码

    Android实现长按圆环动画View效果的思路代码

    这篇文章主要介绍了Android实现长按圆环动画View效果,本文给大家分享实现思路,通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Kotlin设计模式之委托模式使用方法详解

    Kotlin设计模式之委托模式使用方法详解

    Kotlin提供了两个本机功能来实现委托模式,第一个是接口委托(例如策略模式),另一种是属性委托,它专注于类成员/属性(例如延迟加载、observable等),它们共同提供了一组丰富而简洁的功能,通过本博客,您将了解在什么情况下使用此模式
    2023-09-09
  • TextView实现图文混合编排的方法

    TextView实现图文混合编排的方法

    这篇文章主要为大家详细介绍了TextView实现图文混合编排的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android基于ImageView绘制的开关按钮效果示例

    Android基于ImageView绘制的开关按钮效果示例

    这篇文章主要介绍了Android基于ImageView绘制的开关按钮效果,结合实例形式分析了Android使用ImageView进行按钮绘制的界面布局、功能实现及相关注意事项,需要的朋友可以参考下
    2017-03-03
  • Android 跨进程通Messenger(简单易懂)

    Android 跨进程通Messenger(简单易懂)

    这篇文章主要介绍了Android Messenger跨进程通的相关资料,非常简单容易理解,对android messenger 进程通讯的相关知识感兴趣的朋友一起学习吧
    2016-08-08
  • Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例

    Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例

    这篇文章主要介绍了Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能的方法,涉及Android多线程编程及图形绘制相关技巧,需要的朋友可以参考下
    2015-12-12
  • Android通用LoadingView加载框架详解

    Android通用LoadingView加载框架详解

    这篇文章主要为大家详细介绍了Android通用LoadingView加载框架的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07

最新评论