Reprint Please famous source:http://blog.csdn.net/forwardyzk/article/details/46636997
We know that Xlistview has a pull-up refresh and a pull-down load, and now simulates a drop-down refresh of Xlistview, so that Scroller also has a title, which controls the display and hiding of titles by swiping.
Let's look at the effect first:
First create the title view
Scroller_header.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Wrap_content " android: Background= "@android: Color/darker_gray" > <textview android:layout_width= "Match_parent" android:layout_height= "100DP" android:gravity= "center_vertical" android:text= "I am the first child" android: Textsize= "25SP"/></linearlayout>
Customizing the caption control
public class Scrollheader extends LinearLayout {private LinearLayout mcontainer; Private Context Mcontext; Public Scrollheader (context context, AttributeSet Attrs) {Super (context, attrs); Mcontext = context; Initview (); } private void Initview () {//initial case Layoutparams LP = new Layoutparams (Android.view.ViewG Roup. Layoutparams.fill_parent, 0); Mcontainer = (linearlayout) layoutinflater.from (Mcontext). Inflate (R.layout.scroller_header, NULL); AddView (Mcontainer, LP); Setgravity (Gravity.bottom); }/** * Sets the height of the display * * @param height */public void setvisiableheight (int height) {if (Height & Lt 0) height = 0; Layoutparams LP = (layoutparams) mcontainer. Getlayoutparams (); Lp.height = height; MCONTAINER.SETLAYOUTPARAMS (LP); }/** * Gets the height of the display * * @return */public int getvisiableheight () { return Mcontainer.getheight (); }}
Starting with the default setting of Mcontainer, the height is 0, which is not displayed by default.
Setvisiableheight dynamically set the height of the caption
Getvisiableheight gets the height of the caption
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > < Com.example.yang.listviewtitle.view.MyScrollView android:layout_width= "Match_parent" android:layout_height= " Match_parent "> <linearlayout android:layout_width=" match_parent "android:layout_height= "Match_parent" android:orientation= "vertical" > <com.example.yang.listviewtitle.view.scrollhead Er android:layout_width= "match_parent" android:layout_height= "Wrap_content"/> <textview android:layout_width= "match_parent" android:layout_height= "100DP" Android:layout_marginbottom= "10DP" android:background= "@android: Color/holo_blue_dark" and Roid:gravity= "center_vertical" android:padding= "10DP" android:text= "I am a 2nd child"/> <textview android:layout_width= "Match_parent" android:layout_height= "100DP" android:layout_marginbottom= "10DP" android:backgr Ound= "@android: Color/holo_blue_dark" android:gravity= "center_vertical" android:padding= "10DP "Android:text=" I am the 3rd child "/> <textview android:layout_width=" Match_parent " android:layout_height= "100DP" android:layout_marginbottom= "10DP" android:backg Round= "@android: Color/holo_blue_dark" android:gravity= "center_vertical" android:padding= "10d P "android:text=" I am the 4th child "/> <textview android:layout_width=" Match_parent " android:layout_height= "100DP" android:layout_marginbottom= "10DP" Android:back Ground= "@android: color/hOlo_blue_dark "android:gravity=" center_vertical "android:padding=" 10DP "Andro id:text= "I am a 5th child"/> <textview android:layout_width= "Match_parent" Android: layout_height= "100DP" android:layout_marginbottom= "10DP" android:background= "@android: color/ Holo_blue_dark "android:gravity=" center_vertical "android:padding=" 10DP "Android oid:text= "I am a 6th child"/> <textview android:layout_width= "Match_parent" Android : layout_height= "100DP" android:layout_marginbottom= "10DP" android:background= "@android: Color /holo_blue_dark "android:gravity=" center_vertical "android:padding=" 10DP "and roid:text= "I am a 7th child"/> <textview android:layout_width= "Match_parent" Androi d:layout_height= "100DP" Android:layout_marginbottom= "10DP" android:background= "@android: Color/holo_blue_dark" Android:gravity= "center_vertical" android:padding= "10DP" android:text= "I am a 8th child"/&G T <textview android:layout_width= "match_parent" android:layout_height= "100DP" Android:layout_marginbottom= "10DP" android:background= "@android: Color/holo_blue_dark" Android Oid:gravity= "center_vertical" android:padding= "10DP" android:text= "I am a 9th child"/> <textview android:layout_width= "match_parent" android:layout_height= "100DP" Android:layout_marginbottom= "10DP" android:background= "@android: Color/holo_blue_dark" and Roid:gravity= "center_vertical" android:padding= "10DP" android:text= "I'm a 10th child"/> & Lt;/linearLayout> </com.example.yang.listviewtitle.view.MyScrollView></LinearLayout>
This uses custom Scrollerview and custom headings in the custom Scrollerview, which should be noted here:
1.Myscrllerview's Child view is LinearLayout, if you want to change to another layout, make the corresponding changes in the code
2. To use a custom caption as the first child view of the Myscrllerview view.
Custom Myscrollerview
public class Myscrollview extends ScrollView {private Scrollheader mheader; Private float mlasty =-1; Private Context Mcontext; private int max_visi_height; Private Scroller Mscroller; Public Myscrollview (context context, AttributeSet Attrs) {Super (context, attrs); Mcontext = context; Max_visi_height = Getscreenheight ()/3; Mscroller = new Scroller (context, new Decelerateinterpolator ()); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); Get Scroller's first child, the one that wraps all the kids. View LinearLayout firstll = (linearlayout) getchildat (0); Mheader = (scrollheader) firstll.getchildat (0); } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); } @Override public boolean ontouchevent (motionevent ev) {if (mlasty = =-1) {mlasty = Ev.getrawy ( ); } switch (ev.getactIon ()) {Case MotionEvent.ACTION_DOWN:mLastY = Ev.getrawy (); Break Case MotionEvent.ACTION_MOVE:final Float DeltaY = Ev.getrawy ()-mlasty; Mlasty = Ev.getrawy (); if (DeltaY > 0) {int height = mheader.getvisiableheight (); if (Max_visi_height > height) {//Update header height mheader.setvisiableheight ((i NT) DeltaY + height); }} break; Case MOTIONEVENT.ACTION_UP:LOG.D ("Myscrollerview", "move_up"); if (mheader.getvisiableheight () > 0) {resetheaderheight (); } break; } return super.ontouchevent (EV); }/** * Gets the height of the screen * * @return */private int getscreenheight () {WindowManager WindowManager = (WindowManager) Mcontext.getsystemService (Context.window_service); Displaymetrics displaymetrics = new Displaymetrics (); Windowmanager.getdefaultdisplay (). Getmetrics (Displaymetrics); return displaymetrics.heightpixels; } private void Resetheaderheight () {int height = mheader.getvisiableheight (); LOG.D ("Myscrollerview", "moving Height:" + (0-height)); Mscroller.startscroll (0, height, 0, 0-height, 400); Invalidate (); } @Override public void Computescroll () {if (Mscroller.computescrolloffset ()) {Mheader.setvisiab Leheight (Mscroller.getcurry ()); Postinvalidate (); } super.computescroll (); }}
Design ideas:
1. Get the title view in the file
2. By listening to slide scrllerview gestures, if you pull down, then calculate the distance of the slide,
3. Move the title view by talking the sliding distance
4. When the hand leaves the screen, use Scroller to translate the title view to the initial position
Get the title view to manipulate in Onmeasure
Get Scroller's first child, the one that wraps all the kids. View linearlayout firstll = (linearlayout) getchildat (0); Mheader = (scrollheader) firstll.getchildat (0);
In the motionevent.move_move of ontouchevent, the specific slide is calculated, and the height of the caption view is set dynamically.
motionevent.move_up in Ontouchevent: Set the title back to its original position
Mscroller.startscroll (0, height, 0, 0-height, .); Invalidate ();
The height of the caption view is set dynamically in the Computescroll () method, otherwise there will be no panning effect.
@Override public void Computescroll () { if (Mscroller.computescrolloffset ()) { Mheader.setvisiableheight (Mscroller.getcurry ()); Postinvalidate (); } Super.computescroll (); }
Slide Distance judgment: Cannot manipulate the screen one-third height, this height can change according to the demand
Rebound time: Now set to 400 milliseconds, this time can be changed according to demand.
The main title of the display: need to change, just change the scroller_header.xml layout.
To this effect has been implemented.
source Download:http://download.csdn.net/detail/forwardyzk/8838455
ScrollView add title and control display and hide