Android Resilient ScrollView simple implementation, with handling conflicts between ScrollView and Listview,gridview

Source: Internet
Author: User

Deal with the conflict between ScrollView and Listview,gridview,

The best way to do this is to inherit the two classes and rewrite their onmeasure methods:

Listview:

ImportAndroid.widget.ListView;/*** Embed the ListView in the ScrollView to let the ListView show it All *@authorJohn **/ Public classMylistviewextendslistview{ PublicMylistview (android.content.Context context,android.util.attributeset attrs) {Super(context, attrs); }    /*** Set does not scroll*/     Public voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most); Super. Onmeasure (Widthmeasurespec, Expandspec); }    }

Gridview:

ImportAndroid.widget.GridView; Public classMyGridViewextendsGridView { PublicMyGridView (Android.content.Context Context, Android.util.AttributeSet attrs) {Super(context, attrs); }    /*** Set does not scroll*/     Public voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most); Super. Onmeasure (Widthmeasurespec, Expandspec); }}

If you are nesting the GridView in a ListView, you can use this method. This conflict has been dealt with simply and practically.

The following is a rewrite of the scrollview to achieve a resilient ScrollView

 /*** Resilient scrollview* for pull-back and bounce-back *@authorJohn*/ Public classOwnscroviewextendsScrollView {Private Static FinalString TAG = "Elasticscrollview"; //move factor, is a percentage, such as the finger moved 100px, then the view will only move 20px//The goal is to achieve a delayed effect   Private Static Final floatMove_factor = 0.4f; //after releasing the finger, the interface returns to the desired animation time in the normal position.   Private Static Final intAnim_time = 300; //ScrollView's Child view is also the only child view of ScrollView   PrivateView Contentview; //y value when the finger is pressed to calculate the moving distance when moving//if no pull-up and drop-down is pressed, the Y-value of the current finger is updated when the finger moves   Private floatStarty; //used to record the normal layout position   PrivateRect Originalrect =NewRect (); //record if you can continue to pull down when you press the finger   Private BooleanCanpulldown =false; //record if you can continue pulling when your finger is pressed   Private BooleanCanpullup =false; //record whether the layout was moved during finger swipe   Private Booleanismoved =false;  PublicOwnscroview (Context context) {Super(context); }        PublicOwnscroview (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidonfinishinflate () {if(Getchildcount () > 0) {Contentview= Getchildat (0); }} @Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {Super. OnLayout (Changed, L, T, R, b); if(Contentview = =NULL)return; //The location information for the unique child control in ScrollView, which remains unchanged throughout the life cycle of the controlOriginalrect.set (Contentview.getleft (), Contentview.gettop (), Contentview. GetRight (), Contentvie   W.getbottom ()); }   /*** Handle pull-up and pull-down logic in touch events*/@Override Public Booleandispatchtouchevent (motionevent ev) {if(Contentview = =NULL) {           return Super. dispatchtouchevent (EV); }       intAction =ev.getaction (); Switch(action) { CaseMotionevent.action_down://determine if you can pull up and pull downCanpulldown =Iscanpulldown (); Canpullup=Iscanpullup (); //record the Y value when pressedStarty =ev.gety ();  Break;  Casemotionevent.action_up:if(!ismoved) Break;//If there is no move layout, the execution is skipped//Turn on animationTranslateanimation Anim =NewTranslateanimation (0, 0, Contentview.gettop (), originalrect.top);                       Anim.setduration (Anim_time);                       Contentview.startanimation (ANIM); //set back to normal layout positioncontentview.layout (Originalrect.left, Originalrect.top, Originalrect.right, Originalrect                       . bottom); //set the flag bit back to falseCanpulldown =false; Canpullup=false; Ismoved=false;  Break;  CaseMotionevent.action_move://In the process of moving, neither scrolling to the extent that it can be pulled up, nor scrolling to the extent that it can be pulled down           if(!canpulldown &&!)Canpullup) {Starty=ev.gety (); Canpulldown=Iscanpulldown (); Canpullup=Iscanpullup ();  Break; }                       //calculate the distance the finger moves           floatNowy =ev.gety (); intDeltaY = (int) (Nowy-starty); //whether the layout should be moved           BooleanShouldmove =(Canpulldown&& deltay > 0)//can be pulled down, and the finger moves down|| (Canpullup && deltay< 0)//can pull up and move the finger up|| (Canpullup && Canpulldown);//either pull-up or pull-down (this situation occurs when the ScrollView wrapped control is smaller than the ScrollView)                       if(shouldmove) {//Calculate offset               intOffset = (int) (DeltaY *move_factor); //move the layout as your finger movesContentview.layout (Originalrect.left, Originalrect.top +offset, originalrect.right, Originalrect.bottom+offset); Ismoved=true;//record moves the layout           }                        Break; default:            Break; }       return Super. dispatchtouchevent (EV); }       /*** Determine if scrolling to the top*/   Private BooleanIscanpulldown () {returngetscrolly () = = 0 | |contentview.getheight ()< GetHeight () +getscrolly (); }       /*** Determine if scrolling to the bottom*/   Private BooleanIscanpullup () {returnContentview.getheight () <= getheight () +getscrolly (); }    }

And then you can use it in XML.

Android Resilient ScrollView simple implementation, with handling conflicts between ScrollView and Listview,gridview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.