让你的字ScrollView、ListView充分伸展

android默认ScrollView、ListView在最顶部的下拉上拉时或底部,未与反弹效应,很僵,让你无法继续拖累,不比iOS至于能否反弹。个人觉得,iOS互动还是略胜一筹。因此,我们已经走在Android在实现根据本功能。看看下面的效果图:


那么我们今天的目标是一句话实现,怎样去做呢

我们还是先看下代码:

package com.xys.flexible;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ScrollView;

public class FlexibleScrollView extends ScrollView {

	private Context mContext;
	private static int mMaxOverDistance = 50;

	public FlexibleScrollView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.mContext = context;
		initView();
	}

	public FlexibleScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		initView();
	}

	public FlexibleScrollView(Context context) {
		super(context);
		this.mContext = context;
		initView();
	}

	private void initView() {
		DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
		float density = metrics.density;
		mMaxOverDistance = (int) (density * mMaxOverDistance);
	}

	@Override
	protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
			int scrollY, int scrollRangeX, int scrollRangeY,
			int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
		return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
				scrollRangeX, scrollRangeY, maxOverScrollX, mMaxOverDistance,
				isTouchEvent);
	}
}

看见没,事实上我们尽管重写了ScrollView,可是我们仅仅改了它的一个方法的一个值!

也就是将overScrollBy中的maxOverScrollY改成了我们自己写的值。

測试布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.xys.flexible.FlexibleScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n......我是字符串......\n" />
    </com.xys.flexible.FlexibleScrollView>

</RelativeLayout>

就是这样一个推断,默认的maxOverScrollY=0。所以我们看不见不论什么效果,仅仅要改为>0的值,就有效果了。事实上我们仅仅是重写了android的父类方法,但它为什么没有实现这种效果,我们就不得而知了~~

以上。


版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-10-18 12:27  mfrbuaa  阅读(253)  评论(0编辑  收藏  举报