ScrollView nested ScrollView or ListView and internal ScrollView or ListView can also slide

Source: Internet
Author: User

1.ScrollView nested ScrollView and internal ScrollView can also slide

(1) ScrollView Inheriting classes

public class Innerscrollview extends ScrollView {Handler Handler;    Public ScrollView Parentscrollview;        Public Innerscrollview (context context, AttributeSet Attrs) {Super (context, attrs);    Handler = new Handler ();    } private int lastscrolldelta = 0;        public void Resume () {Overscrollby (0,-lastscrolldelta, 0, getscrolly (), 0, Getscrollrange (), 0, 0, true);    Lastscrolldelta = 0;    } int mtop = 10; /** * Roll TargetView to the top */public void ScrollTo (final View targetview) {handler.postdelayed (new Runnable                () {@Override public void run () {int oldscrolly = getscrolly ();                int top = Targetview.gettop ()-mtop;                Final int delaty = top-oldscrolly;                Lastscrolldelta = Delaty;            Smoothscrollto (0, top);    }}, 50);        } private int Getscrollrange () {int scrollrange = 0;          if (Getchildcount () > 0) {  View child = Getchildat (0);        Scrollrange = Math.max (0, Child.getheight ()-(GetHeight ()));    } return Scrollrange;    } int currenty; @Override public boolean onintercepttouchevent (motionevent ev) {if (Parentscrollview = = null) {Retu        RN super.onintercepttouchevent (EV);                } else {if (ev.getaction () = = Motionevent.action_down) {//to intercept the parent ScrollView's scrolling event                CurrentY = (int) ev.gety ();                Setparentscrollable (FALSE);            return super.onintercepttouchevent (EV); } else if (ev.getaction () = = motionevent.action_up) {//Restores the scrolling event to the parent ScrollView Setparentscroll            Able (TRUE); } else if (ev.getaction () = = Motionevent.action_move) {}} return Super.onintercepttouchevent (EV    );        } @Override public boolean ontouchevent (motionevent ev) {View child = getchildat (0);   if (Parentscrollview! = null) {         if (ev.getaction () = = motionevent.action_move) {int height = child.getmeasuredheight ();                Height = height-getmeasuredheight ();                System.out.println ("height=" + height);                int scrolly = getscrolly ();                System.out.println ("scrolly" + scrolly);                int y = (int) ev.gety (); Swipe down if (CurrentY < y) {if (scrolly <= 0) {//Swipe down                        To the head, roll over to the parent ScrollView setparentscrollable (true);                    return false;                    } else {setparentscrollable (false); }} else if (CurrentY > Y) {if (scrolly >= height) {//IF                        Swipe up to the head and roll over to the parent ScrollView setparentscrollable (true);                    return false; } else {SetparEntscrollable (FALSE);            }} currenty = y;    }} return super.ontouchevent (EV);        }/** * Whether to give the rollover event to the parent ScrollView * * @param flag */private void Setparentscrollable (Boolean flag) {    Parentscrollview.requestdisallowintercepttouchevent (!flag); }}

(2) ScrollView using activity

public class Scrollviewactivity extends Activity {ScrollView scrollView1;    Innerscrollview Innerscrollview;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        ScrollView1 = (ScrollView) Findviewbyid (R.id.scroll_1);        Innerscrollview = (Innerscrollview) Findviewbyid (r.id.scroll_2);        Innerscrollview.parentscrollview = ScrollView1;        Final button button = (button) Innerscrollview.findviewbyid (R.id.scroll_button2);        Final View content = Findviewbyid (r.id.scroll_content);                Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                    if (content.getvisibility () = = view.visible) {innerscrollview.resume ();                Content.setvisibility (View.gone);                    } else {Innerscrollview.scrollto (v); Content.setvisibility (view.visible);    }            }        }); }}

Nested ListView in 2.ScrollView and internal ListView can also slide

(1) ScrollView Inheriting classes

public class Innerlistview extends ListView {ScrollView parentscrollview;    Public ScrollView Getparentscrollview () {return parentscrollview;    } public void Setparentscrollview (ScrollView parentscrollview) {this.parentscrollview = Parentscrollview;    } private int maxheight;    public int getmaxheight () {return maxheight;    } public void Setmaxheight (int maxheight) {this.maxheight = MaxHeight;    } public Innerlistview (context context, AttributeSet Attrs) {Super (context, attrs); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated Me Thod stub if (MaxHeight >-1) {Heightmeasurespec = Measurespec.makemeasurespec (MaxHeight, Measurespe        C.at_most);        } super.onmeasure (Widthmeasurespec, Heightmeasurespec);    System.out.println (Getchildat (0)); } @Override public boolean onintercepttouchevent (Motionevent ev) {sWitch (Ev.getaction ()) {case MotionEvent.ACTION_DOWN:setParentScrollAble (false);            Case MotionEvent.ACTION_MOVE:break;                Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:setParentScrollAble (true);            Break        Default:break;    } return super.onintercepttouchevent (EV); }/** * @param flag */private void Setparentscrollable (Boolean flag) {Parentscrollview.requestdisal    Lowintercepttouchevent (!flag); }}

(2) ScrollView Practical Activity

public class Listviewactivity extends Activity {    ScrollView ScrollView;    Innerlistview ListView;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.list_view_test);        ListView = (Innerlistview) Findviewbyid (r.id.list);        ScrollView = (ScrollView) Findviewbyid (R.id.scroll_view);        arrayadapter<string> adapter = new Arrayadapter<string> (this,                Android. R.layout.select_dialog_singlechoice);        for (int i = 0; i < i++) {            Adapter.add (string.valueof (i));        }        Listview.setadapter (adapter);        Listview.setcachecolorhint (0x00000000);        Listview.setbackgrounddrawable (null);        Listview.setbackgroundcolor (color.white);        Listview.setparentscrollview (ScrollView);        Listview.setmaxheight (+);    }}

Source Address: http://download.csdn.net/detail/pcaxb/8981295

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ScrollView nested ScrollView or ListView and internal ScrollView or ListView can also slide

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.