Android ScrollView sliding up control top floating effect implementation
I would like to express my gratitude for the code of "the floating box at the top of the slide dock. The implementation method in [floating frame at the top of the slide dock] is to use two controls. When sliding, listen to the scrolling Y value of ScrollView, in this way, the top of the control is suspended by hiding the display of the two controls. However, in actual application scenarios, there may be a large amount of content in the control to be suspended. If this is achieved by displaying hidden content, two sets of variables need to be defined repeatedly. It is very troublesome to modify the content in the control.
The methods in this article are implemented through addView and removeView.
1. First, let ScrollView implement rolling listening:
Package com. willen. topFloatDemo; import android. content. context; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. widget. scrollView;/** the ScrollView does not implement a rolling listener, so we must monitor the ScrollView on our own. * we naturally think of onTouchEvent () method To monitor * ScrollView's rolling Y value */public class MyScrollView extends ScrollView {private OnScrollListener onScrollListener;/*** mainly used When the user's finger leaves MyScrollView, MyScrollView continues to slide. We use it to save the distance of Y and then compare it with */private int lastScrollY; public MyScrollView (Context context) {super (context, null);} public MyScrollView (Context context, AttributeSet attrs) {super (context, attrs, 0);} public MyScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}/*** set the rolling interface * @ param onScrollListener */public void setOnScrollLis Tener (OnScrollListener onScrollListener) {this. onScrollListener = onScrollListener;}/*** is used to obtain the Y distance of MyScrollView scrolling when the user's finger leaves MyScrollView, and then calls back to */private Handler handler = new Handler () in the onScroll method () {public void handleMessage (android. OS. message msg) {int scrollY = MyScrollView. this. getScrollY (); // The distance at this time is not the same as the recorded distance. Send a message to handler in 5 milliseconds if (lastScrollY! = ScrollY) {lastScrollY = scrollY; handler. sendMessageDelayed (handler. obtainMessage (), 5);} if (onScrollListener! = Null) {onScrollListener. onScroll (scrollY) ;}};/*** override onTouchEvent. When the user's hand is on the MyScrollView, * directly call back the Y-direction distance of MyScrollView to the onScroll method. When the user raises his hand, * MyScrollView may still slide, so when the user raises his hand, we send a message to handler every Five milliseconds, and process * MyScrollView sliding distance */@ Overridepublic boolean onTouchEvent (MotionEvent ev) {if (onScrollListener! = Null) {onScrollListener. onScroll (lastScrollY = this. getScrollY ();} switch (ev. getAction () {case MotionEvent. ACTION_UP: handler. sendMessageDelayed (handler. obtainMessage (), 20); break;} return super. onTouchEvent (ev);}/*** rolling callback interface */public interface OnScrollListener {/*** callback method, returns the Y distance of MyScrollView sliding */public void onScroll (int scrollY );}}
Ii. Define a simple layout
3. MainActivity
Package com. willen. topFloatDemo; import android. app. activity; import android. OS. bundle; import android. widget. editText; import android. widget. linearLayout; import android. widget. relativeLayout; import com. willen. topFloatDemo. myScrollView. onScrollListener; public class MainActivity extends Activity implements OnScrollListener {private EditText search_edit; private MyScrollView myScrollView; private int searc HLayoutTop; LinearLayout search01, search02; RelativeLayout rlayout; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the control init ();} private void init () {search_edit = (EditText) findViewById (R. id. search_edit); myScrollView = (MyScrollView) findViewById (R. id. myScrollView); search01 = (LinearLayout) findViewById (R. id. se Arch01); search02 = (LinearLayout) findViewById (R. id. search02); rlayout = (RelativeLayout) findViewById (R. id. rlayout); myScrollView. setOnScrollListener (this) ;}@ Overridepublic void onWindowFocusChanged (boolean hasFocus) {super. onWindowFocusChanged (hasFocus); if (hasFocus) {searchLayoutTop = rlayout. getBottom (); // get the top position of searchLayout} // listen for Rolling Y value changes. The hover effect is achieved through addView and removeView @ Overridepublic void onScroll (in T scrollY) {if (scrollY> = searchLayoutTop) {if (search_edit.getParent ()! = Search01) {search02.removeView (search_edit); search01.addView (search_edit) ;}} else {if (search_edit.getParent ()! = Search02) {search01.removeView (search_edit); search02.addView (search_edit );}}}}