Android view scrolling, stretching to the top/bottom resilient rebound reset

Source: Internet
Author: User

"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 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://"    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>


Package Zhangphil.view;import Zhangphil.view.r;import;import Android.widget.textview;import;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 (;//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 (;}}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.