This article refers to: "Top slide docked at the end of the floating box" code, here to express thanks. The "hover box on top slide" is implemented by using two controls that, when sliding, listen for the scroll y value of the scrollview, thus enabling the top suspension of the control by hiding the display of the two controls. However, in the actual application scenario, it may be necessary to hover the content of the control inside is more, if it is implemented by showing hidden way, the operation of the contents of the control, you need to repeatedly define two sets of variables, the contents of the control to modify the content is also to operate again, very troublesome.
The method of this paper is implemented by AddView and Removeview.
First, let the ScrollView realize the rolling monitoring:
Package Com.willen.topfloatdemo;import Android.content.context;import Android.os.handler;import Android.util.attributeset;import Android.view.motionevent;import android.widget.scrollview;/* * ScrollView does not implement scrolling monitoring, so we have to implement our own monitoring of the scrollview, * we naturally think in the Ontouchevent () method to implement the rolling y-axis monitoring * ScrollView scroll y value to listen to the * * public class Myscrollview extends ScrollView {private Onscrolllistener onscrolllistener; /** * Mainly used in the user's finger left Myscrollview,myscrollview still continue to slide, we used to save the distance of Y, then do compare */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 Scrolling interface * @param onscrolllistener */public void Setonscrolllistener (Onscrolllistener onscrolllistener) {this.onscrolllistener = Onscrolllistener;} /** * Used to get myscrollview scrolling y distance when the user fingers leave Myscrollview, then callback to Onscroll method */private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) { int scrolly = MyScrollView.this.getScrollY (); The distance at this time is not equal to the distance recorded, sending a message to handler in 5 milliseconds if (lastscrolly! = scrolly) {lastscrolly = scrolly; Handler.sendmessagedelayed (Handler.obtainmessage (), 5); } if (Onscrolllistener! = null) {Onscrolllistener.onscroll (scrolly); } }; }; /** * Rewrite ontouchevent, when the user's hand is above the Myscrollview, * direct the Myscrollview sliding y-direction distance back to the Onscroll method, when the user raises his hands, * Myscrol LView may still be sliding, so when the user lifts his hand we send a message to handler at 5 milliseconds, the distance in handler processing * myscrollview Sliding */@Overridepublic boolean ontouchevent (Mo Tionevent 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);} /** * Scrolling Callback interface */public interface onscrolllistener{/** * Callback method, returns the Y-direction distance of the Myscrollview slide */public void onscroll (int scrolly); }}
Second, define simple layout
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height= "Match_parent" android:orientation= "vertical" > <com.willen.topfloatdemo.myscrollview android:id= "@+id/ Myscrollview "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Linea Rlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android: orientation= "vertical" > <relativelayout android:id= "@+id/rlayout" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "cen Ter_horizontal "> <textview android:id=" @+id/tv "android:layout _width= "Wrap_content" Androidoid:layout_height= "Wrap_content" android:gravity= "center_vertical" android:text= "top letter The top message \ n Top information \ n Top Information "android:textsize=" 40DP "/> </RelativeLayout> <li Nearlayout android:id= "@+id/search02" android:layout_width= "Match_parent" and roid:layout_height= "40dip" android:orientation= "vertical" > <edittext Android:id= "@+id/search_edit" android:layout_width= "Match_parent" Android:layout_ height= "40dip" android:background= "@drawable/bg_edittext" android:hint= "Please enter ..." android:padding= "5dip" android:singleline= "true" Android:textcolorhin t= "#AAAAAA" android:textsize= "15dip"/> </LinearLayout> <textview Android:layoUt_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizon Tal "android:text=" test content \ n Test content \ n Test content \ n Test content \ n Test content \ n Test content \ n Tests content \ n Test content \ n Tests contents \ nthe test contents \ nto test content \ n Testing Contents \ n Test content \ n Test content \ n Test content \ n Test content \ n Test content \ n Test content \ n Tests content \ n Test content \ n Test content \ n Test content \ n Testing content \ n Test Contents "android:textsize=" 40DP "/> </LinearLayout> </com.willen.topFloatDemo.MyScrollView> <linearlayout android:id= "@+id/sea Rch01 "android:layout_width=" match_parent "android:layout_height=" 40dip "android:orientation=" Vertica L "> </LinearLayout></RelativeLayout>
Third, 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 searchlayouttop; LinearLayout search01,search02; Relativelayout rlayout; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//Initialize Control init ();} private void Init () {Search_edit = (EditText) Findviewbyid (r.id.search_edit); Myscrollview = (Myscrollview) Findviewbyid (r.id.myscrollview); search01 = (linearlayout) Findviewbyid (r.id.search01); 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 ();//Gets the top position of the Searchlayout}}//listens for changes in the scroll Y value, via AddView and Removevie W to implement the hover effect @overridepublic void onscroll (int 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);} }}}
There are comments in the code and should not be explained more.
This article source code download: http://download.csdn.net/detail/viviwen123/7989349
Android ScrollView slide up control top hover effect implementation