Android v4 SwipeRefreshLayout的二次开发,支持背景层View一起滑动.

本人使用的是版本为21.0.2的v4包.这个版本的下拉和以前的版本不同,它下拉的时候会出现一个圆球,送手时 圆球会进行动画.SwipeRefreshLayout类包含了2个私有类,一个是MaterialProgressDrawable(最中心的动画);一个是CircleImageView(圆球),它继承了ImageView,而MaterialProgressDrawable继承了Drawable.所以MaterialProgressDrawable为CircleImageView所显示的内容.它在构造函数的时候调用了createProgressView()方法.来看关键代码:

	private void createProgressView() {
		mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT,
				CIRCLE_DIAMETER / 2);
		mProgress = new MaterialProgressDrawable(getContext(), this);
		mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);
		mCircleView.setImageDrawable(mProgress);
		mCircleView.setVisibility(View.GONE);
		addView(mCircleView);
	}
这里开始讲如何将它支持下拉时,内容View一起滚动的效果类似 Android -PullToRefresh,但又保留自带的动画效果.实现起来很简单.重写onLayout:

@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		final int width = getMeasuredWidth();
		final int height = getMeasuredHeight();
		if (getChildCount() == 0) {
			return;
		}
		if (mTarget == null) {
			ensureTarget();
		}
		if (mTarget == null) {
			return;
		}
		int circleWidth = mCircleView.getMeasuredWidth();
		int circleHeight = mCircleView.getMeasuredHeight();
		final View child = mTarget;
		final int childLeft = getPaddingLeft();
		int childTop = getPaddingTop();
		if (mCurrentTargetOffsetTop != -circleHeight
				&& mCurrentTargetOffsetTop < 0) {
			childTop += circleHeight + mCurrentTargetOffsetTop;
		} else if (mCurrentTargetOffsetTop != -circleHeight
				&& mCurrentTargetOffsetTop >= 0) {
			childTop += mCurrentTargetOffsetTop + circleHeight
					+ mCurrentTargetOffsetTop;
		}
		final int childWidth = width - getPaddingLeft() - getPaddingRight();
		final int childHeight = height - getPaddingTop() - getPaddingBottom();
		mCircleView.layout((width / 2 - circleWidth / 2),
				mCurrentTargetOffsetTop, (width / 2 + circleWidth / 2),
				mCurrentTargetOffsetTop + circleHeight);
		child.layout(childLeft, childTop, childLeft + childWidth, childTop
				+ childHeight);
	}
这样内容View就与下拉View同时滚动了.当然你也可以在onLayout中设置Imageview的位置,很简单吧.赶紧试试吧.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值