Android View scroll, stretch to top/bottom Elastic Rebound reset, androidview
Zookeeper
Android View rolling, stretching to top/bottom Elastic Rebound Reset
In the previous article, I introduced how to implement auto-Rebound reset like a rubber band after an Android ListView is pulled to the top/bottom. article link: http://blog.csdn.net/zhangphil/article/details/47311155 ). In fact, all controls of Android packaged by ScrollView can achieve the interactive design effect of rolling to the top/bottom and elastic rebound reset. The key point is to override the overScrollBy () method of the Android native ScrollView.
The implementation code and steps are as follows:
(1) first, you need to write a View that inherits from ScrollView, and then rewrite the key method: overScrollBy (). Suppose this 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 control of the ScrollView package out of the distance from the top or bottom. Private static final int MAX_OVERSCROLL_Y = 200; private Context mContext; private int newMaxOverScrollY; public ZhangPhilScrollView (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); thi S. 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 scroll bar of the ScrollView. This. setVerticalScrollBarEnabled (false); // no matter whether the data filled by the loaded control is full or not, the elastic rebound with the same rubber band is allowed. This. setOverScrollMode (ScrollView. OVER_SCROLL_ALWAYS);} // the most critical point. // Add @ SuppressLint ("NewApi") to support SDK8 "). @ Override ("NewApi") @ override 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 use the ZhangPhilScrollView package in the layout file code like the native ScrollView of Android to implement the auto-Rebound interactive design component. In my example, for the simple purpose, assume that ZhangPhilScrollView contains only one 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) findV IewById (R. id. text); // test the dataset. String s = ""; for (int I = 0; I <10; I ++) {s + = I + "\ n";} text. setText (s); // set the background color of TextView, which makes it easier to observe the elastic rebound effect. Text. setBackgroundColor (Color. RED );}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Reprinted please indicate the source: http://blog.csdn.net/zhangphil