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

以上。


更多相关文章

  1. Galaxy S8及S+全屏显示适配
  2. 当透明状态栏遇到输入框
  3. 模仿iOS版微信的滑动View效果
  4. Android实现加载富文本以及SpannableString、SpannableStringBui
  5. Android中实现程序前后台切换效果。
  6. 【Android游戏开发十四】深入Animation,在SurfaceView中照样使用
  7. Android(安卓)的UI效果篇Ripple
  8. 【Android(安卓)开发】:UI控件之 ViewPager 多页面滑动效果控件的
  9. Android设置Alpha值实现图片渐变效果

随机推荐

  1. 实训心得___刘晓刚
  2. AndroidAPP开发者注意了,安卓开发将迎来
  3. android中关于AsyncTask的学习
  4. 【Android教程】Android用户系统管理
  5. 基于xml类型的压缩数据流的android获取天
  6. Android使用Http连接服务器,解析JSON, XML
  7. Android消息处理机制:Handler|Message
  8. Android分辨率
  9. Android(安卓)主题切换功能
  10. android中使用selector动态改变imageView