"Android view scrolling, stretching to the top/bottom resilient rebound reset"
I explained in an article on how to implement an Android ListView pull to the top/bottom, like a rubber band elastic rebound Reset ("Android ListView pulls to the top/bottom, like a rubber band elastic rebound reset", article link address:/http blog.csdn.net/zhangphil/article/details/47311155). In fact, Android is usually wrapped by scrollview control, can achieve scroll to the top/bottom, elastic rebound reset interactive design effect. The key point is to rewrite the Android native ScrollView Overscrollby () method.
Now give the implementation code and steps:
(1) First you need to write a view to inherit from ScrollView, then rewrite the key method: Overscrollby (). Suppose the view is called Zhangphilscrollview:
Package Zhangphil.view;import Android.annotation.suppresslint;import Android.content.context;import Android.util.attributeset;import Android.util.displaymetrics;import Android.widget.scrollview;public Class Zhangphilscrollview extends scrollview{//This value control can pull the ScrollView wrapped control out of the distance from the top or bottom. private static final int max_overscroll_y = 200;private Context mcontext;private int newmaxoverscrolly;public ZHANGPHILSC Rollview (Context context) {super (context); init (context);} Public Zhangphilscrollview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} /* Public Zhangphillistview (context context, AttributeSet attrs, int * defstyle) {Super (context, attrs, Defstyle); . Mcontext = Context; * Init (); } */@SuppressLint ("Newapi") private void init (context context) {This.mcontext = Context;displaymetrics metrics = Mcontext . Getresources (). Getdisplaymetrics (); float density = metrics.density;newmaxoverscrolly = (int) (Density * max_ overscroll_y);//false: Hides the ScrollView scroll bar. This.setverticAlscrollbarenabled (false);//The elastic rebound of a rubber band is allowed, regardless of whether the loaded control fills the full screen. This.setoverscrollmode (scrollview.over_scroll_always);} The most critical place. Support to SDK8 need to add @suppresslint ("Newapi"). @SuppressLint ("Newapi") @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, newmaxoverscrolly,istouchevent);}}
(2) Then in the layout file code like the use of Android native ScrollView using the Zhangphilscrollview package needs to implement elastic rebound interaction design components, in my case, for simple purposes, Suppose the Zhangphilscrollview parcel is just a textview:
<zhangphil.view.zhangphilscrollview xmlns:android= "Http://schemas.android.com/apk/res/android" android: Layout_width= "Match_parent" android:layout_height= "match_parent" > <textview android:id= "@+id /text " android:layout_width=" match_parent " android:layout_height=" Wrap_content "/></ Zhangphil.view.zhangphilscrollview>
Test:
Package Zhangphil.view;import Zhangphil.view.r;import Android.support.v7.app.actionbaractivity;import Android.widget.textview;import Android.graphics.color;import Android.os.bundle;public class MainActivity extends actionbaractivity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView Text = (TextView) Findviewbyid (r.id.text);//test data set. String s = ""; for (int i = 0; I < 10; i++) {s + = i + "\ n";} Text.settext (s);//Set the background color of the textview to make it easier to see the elastic rebound effect. Text.setbackgroundcolor (color.red);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android view scrolling, stretching to the top/bottom resilient rebound reset