Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/28441197
Now many apps have added a rebound effect to ScrollView, QQ, MI private SMS and so on. Just see a class on the net: Bouncescrollview,
Original address is: http://blog.csdn.net/h7870181/article/details/8960430, unfortunately the author did not provide a, so I found that the Millet Text message list page DOWN, there is a rebound effect, and pull to more than 1/3, will open the private SMS list, Xiaomi users can try.
I modified the author in the Bouncescrollview class, wrote an example, to share with you.
: (Simulator effect is not good, can see the effect)
1, the first is the layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <com.example.zhy_bouncescrollview02. Bouncescrollview android:background= "@drawable/shakehideimg_man" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:id= "@+id/id_scrollview" > <com.example.zhy_ Bouncescrollview02. Mylistview android:background= "#fff" android:id= "@+id/id_listview" android:layout_width= "Fill_ Parent " android:layout_height=" wrap_content "> </com.example.zhy_bouncescrollview02. Mylistview> </com.example.zhy_bouncescrollview02. Bouncescrollview></relativelayout>
A custom ScrollView contains a custom listview that customizes ScrollView to add bounce effects, customizing the ListView to address problems caused by ScrollView and ListView nesting.
2, Bouncescrollview
Package Com.example.zhy_bouncescrollview02;import Android.content.context;import Android.graphics.rect;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.translateanimation;import android.widget.scrollview;/** * Support up and down bounce effect ScrollView * * @author Zhy * */public class Bouncescrollview extends ScrollView {Private Boolean iscalled; private Callback mcallback;/** * contains view */private view mview;/** * Position when stored normally */private Rect MR ECT = new Rect ();/** * y-coordinate */private int y;private Boolean isFirst = True;public Bouncescrollview (context context, Attribu Teset attrs) {Super (context, attrs);} /*** * Work is done based on the XML generation view. The function is called at the end of the build view, after all the child views have been added. Even if the subclass overrides the Onfinishinflate * method, the method of the parent class should be called to enable the method to execute. */@Overrideprotected void Onfinishinflate () {if (Getchildcount () > 0) mView = getchildat (0); Super.onfinishinflate ();} @Overridepublic boolean ontouchevent (motionevent ev) {if (MView! = null) {Commonontouch (EV);}return super.ontouchevent (EV);} private void Commonontouch (Motionevent ev) {int action = ev.getaction (); int cy = (int) ev.gety (); switch (action) {case Motio nevent.action_down:break;/** * Follow finger move */case MotionEvent.ACTION_MOVE:int dy = cy-y;if (isFirst) {dy = 0;isfirst = false;} y = cy;if (Isneedmove ()) {if (Mrect.isempty ()) {/** * records the position before Movement */mrect.set (Mview.getleft (), Mview.gettop (), Mview.getright (), Mview.getbottom ());} Mview.layout (Mview.getleft (), mview.gettop () + 2 * dy/3,mview.getright (), Mview.getbottom () + 2 * DY/3); if (ShouldCall Back (DY) {if (mcallback! = null) {if (!iscalled) {iscalled = true; Resetposition (); Mcallback.callback ();}}}} break;/** * Bounce back */case MotionEvent.ACTION_UP:if (!mrect.isempty ()) {resetposition ();} Break;}} /** * When moving from the top down to half the distance, the callback interface * * @return */private boolean shouldcallback (int dy) {if (dy > 0 && mview.gettop () & Gt GetHeight ()/2) return True;return false;} private void Resetposition () {Animation Animation = new Translateanimation (0, 0, Mview.gettop (), mrect.top); animation.setduration (+); Animation.setfillafter (true); mview.startanimation (animation); Mview.layout (Mrect.left, Mrect.top, Mrect.right, Mrect.bottom); Mrect.setempty (); IsFirst = true;iscalled = false; }/*** * Whether to move layout inner.getmeasuredheight (): Gets the total height of the control * * getheight (): Gets the height of the screen * * @return */public boolean isneedmove ( {int offset = mview.getmeasuredheight ()-getheight (), int scrolly = getscrolly (),//0 is the top, and the back one is the bottom if (scrolly = 0 | | scrol LY = = offset) {return true;} return false;} public void Setcallback (Callback Callback) {mcallback = Callback;} Interface Callback{void Callback ();}}
The main is to listen to Ontouchevent, when the move, the control in the ScrollView follow the finger move, up to restore the original position, when the 1/2, the user set callback will be called, the details of their own code to see.
3, Mylistview
Package Com.example.zhy_bouncescrollview02;import Android.content.context;import Android.util.attributeset;import android.widget.listview;/** * Fix ScrollView and ListView nesting issues * @author Zhy * */public class Mylistview extends listview{ Public Mylistview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Mylistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Mylistview (Context context) {super (context);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {/** * resolves nested problems with ScrollView and ListView */int Expandspec = Measurespec.makemeasurespec (integer.max_value >> 2,measurespec.at_most); Super.onMeasure ( Widthmeasurespec, Expandspec);}}
4. Main activity
Package Com.example.zhy_bouncescrollview02;import Java.util.arraylist;import Java.util.arrays;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.widget.ArrayAdapter; Import Android.widget.listview;import Android.widget.toast;import com.example.zhy_bouncescrollview02. Bouncescrollview.callback;public class Mainactivity extends Activity{private ListView mlistview;private Bouncescrollview Mscrollview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Mscrollview = (Bouncescrollview) findViewById (R.id.id_ ScrollView) Mscrollview.setcallback (new Callback () {@Overridepublic void Callback () {Toast.maketext ( Mainactivity.this, "Can do something!", 0). Show (); Intent Intent = new Intent (mainactivity.this,secondactivity.class ); StartActivity (intent); Overridependingtransition (r.anim.fade_in, r.anim.fade_out);}); Mlistview = (ListView) Findviewbyid (R.id.id_listview); MlistvieW.setadapter (New arrayadapter<string> (this,android. R.layout.simple_list_item_1, New arraylist<string> (Arrays.aslist ("Hello", "World", "Welcome", "Java", "Android "," Lucene "," C + + "," C # "," HTML "," Welcome "," Java "," Android "," Lucene "," C + + "," C # "," HTML ")))));}}
The Mainactivity code is also simple, initializes two controls, and sets the callback of the lower ScrollView.
Well, I haven't written the code for a few days, so that's it.
SOURCE Click to download