The default scrollview and listview of Android do not show a bounce effect when they are pulled from the top or bottom of the screen. It is so stiff that you cannot continue to drag, unlike iOS, I personally think that IOS interaction is a little better, so we can also implement this function in Android. Let's take a look at it first:
So our goal today is to achieve it in one sentence. How can we do it?
Let's take a look at the Code:
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);}}
I can't see it. In fact, although we have rewritten scrollview, we only changed the value of one of its methods!
That is, we changed maxoverscrolly in overscrollby to our own value.
Test layout:
<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/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/plugin" 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 = "...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n ...... I am a string ...... \ n "/> </COM. xys. flexible. flexiblescrollview> </relativelayout>
The default value is maxoverscrolly = 0, so we cannot see any effect. If we change it to a value greater than 0, it will have an effect. In fact, we just override the parent class method of Android, however, we don't know why it didn't achieve this effect ~~
Above.
One sentence makes your scrollview and listview fully scalable