To implement a function: When Scrollview slides to the bottom, the event needs to be triggered to load other data. Many people think that ScrollView can be setOnScrollListener like ListViev. In fact, it is not that simple, because ScrollView does not have this interface at all, and no proper answer is found for a circle in baidu, there is no way I can only go to google. I solved this problem all of a sudden, but it was quite cool for foreigners. Haha, this is my website:
Http://stackoverflow.com/questions/2864563/how-do-i-know-that-the-scrollview-is-already-scrolled-to-the-bottom
Note: if the data is less than one page, the onBottom method will be executed! Normally, if you want to use lazy loading, there will be more than one page of data, so I did not carefully consider this issue!
I encapsulated ScrollView as a class. The source code is as follows:
Package com. ql. view; import android. content. context; import android. OS. handler; import android. OS. message; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. scrollView; public class LazyScrollView extends ScrollView {private static final String tag = "LazyScrollView"; private Handler handler; private View view; public LazyScrollView (Context contex T) {super (context); // TODO Auto-generated constructor stub} public LazyScrollView (Context context, AttributeSet attrs) {super (context, attrs ); // TODO Auto-generated constructor stub} public LazyScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // TODO Auto-generated constructor stub} // obtain the total height public int computeVerticalScrollRange () {return super. computeHor IzontalScrollRange ();} public int computeverticalscroloffset () {return super. computeverticalscroloffset ();} private void init () {this. setOnTouchListener (onTouchListener); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// process incoming messages heresuper. handleMessage (msg); switch (msg. what) {case 1: if (view. getMeasuredHeight () <= getScrollY () + getHeight () {if (onScrollListene R! = Null) {onScrollListener. onBottom () ;}} else if (getScrollY () = 0) {if (onScrollListener! = Null) {onScrollListener. onTop () ;}} else {if (onScrollListener! = Null) {onScrollListener. onScroll () ;}} break; default: break ;}};} OnTouchListener onTouchListener = new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event. getAction () {case MotionEvent. ACTION_DOWN: break; case MotionEvent. ACTION_UP: if (view! = Null & onScrollListener! = Null) {handler. sendMessageDelayed (handler. obtainMessage (1), 200);} break; default: break;} return false ;};/*** get the reference View, mainly to get its MeasuredHeight, then compare it with ScrollY + getHeight of the scroll bar. */Public void getView () {this. view = getChildAt (0); if (view! = Null) {init () ;}}/*** defined interface * @ author admin **/public interface OnScrollListener {void onBottom (); void onTop (); void onScroll ();} private OnScrollListener onScrollListener; public void setOnScrollListener (OnScrollListener onScrollListener) {this. onScrollListener = onScrollListener ;}}
It is also very easy to use. It is usually used as follows:
ScrollView = (LazyScrollView) findViewById (R. id. scrollView); scrollView. getView (); scrollView. setOnScrollListener (new OnScrollListener () {@ Overridepublic void onTop () {// TODO Auto-generated method stubLog. d (tag, "------ scroll to the top ------") ;}@ Overridepublic void onScroll () {// TODO Auto-generated method stubLog. d (tag, "neither to the bottom nor to the top");} @ Overridepublic void onBottom () {// TODO Auto-generated method stubLog. d (tag, "------ scroll to the bottom ------");}});