首页 技术 正文
技术 2022年11月19日
0 收藏 957 点赞 3,049 浏览 2831 个字

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

让你的字ScrollView、ListView充分伸展

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

我们还是先看下代码:

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的父类方法,但它为什么没有实现这种效果,我们就不得而知了~~

以上。

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898