Android scrollview Slide Fixed a control (US Buy details UI) Perfect Edition

Source: Internet
Author: User

Slide Fixed a control on ScrollView (US Buy details UI) This article describes how to use Touchlistener to achieve similar sliding stop effect, but there is an obvious bug in this method, that is, when the content is more, Most people are sliding to see the content, rather than touch the way, which will lead to the top of the slider is not timely, or deferred phenomenon, here is a new way to achieve similar results, can be a good solution to the above problems.

At present, there is no onscrolllistener in ScrollView, so we need to implement it, first make a scrollview:

Package Com.example.meituandemo;import Android.content.context;import Android.util.attributeset;import Android.widget.scrollview;public class Myscrollview extends ScrollView {private Onscrolllistener onscrolllistener; Public Myscrollview (Context context) {This (context, null);} Public Myscrollview (context context, AttributeSet Attrs) {This (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;} @Overrideprotected void onscrollchanged (int l, int t, int oldl, int Oldt) {//Slide change will call super.onscrollchanged in real time (L, T, OLDL, O LDT); if (onscrolllistener! = null) {Onscrolllistener.onscroll (t);}} /** * * Scrolling Callback interface * */public Interface onscrolllistener{/** * Callback method, return Myscrollview sliding y-direction distance * @param scrolly *, */public VO ID onscroll (int scrolly);}}

then it is called in mainactivity:

Package Com.example.meituandemo;import Android.app.activity;import Android.os.bundle;import Android.view.viewtreeobserver.ongloballayoutlistener;import Android.widget.linearlayout;import Com.example.meituandemo.myscrollview.onscrolllistener;public class Mainactivity extends Activity implements onscrolllistener{//Note inherited custom listener/** * Custom Myscrollview */private myscrollview myscrollview;/** * Purchase layout inside Myscrollview */private linearlayout mbuylayout;/** * At the top of the purchase layout */private LinearLayout mtopbuylayout;@ Suppresswarnings ("deprecation") @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Myscrollview = (Myscrollview) Findviewbyid (R.id.scrollview);// Your scrollviewmbuylayout = (linearlayout) Findviewbyid (r.id.buy);//sliding purchase layout mtopbuylayout = (linearlayout) findViewById ( R.id.top_buy_layout)///top of the purchase layout myscrollview.setonscrolllistener (this);////When the state of the layout or the visibility of the control changes the interface of the callback Findviewbyid ( r.id.parent_layout). Getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {//@Overridepublic void Ongloballayout () {//This step is important, Make the above purchase layout coincident with the purchase layout below Onscroll (myscrollview.getscrolly ());});} @Overridepublic void onscroll (int scrolly) {//This is a callback interface call function, the layout function is a method of view redraw, the effect is that when the following purchase layout is not sliding to the top, the two layouts are drawn together, A similar effect is achieved when the purchase layout below slides out of the screen, drawing the top purchase layout to the top without moving. int mbuylayout2parenttop = Math.max (scrolly, Mbuylayout.gettop ()); Mtopbuylayout.layout (0, Mbuylayout2parenttop, Mtopbuylayout.getwidth (), Mbuylayout2parenttop + mtopbuylayout.getheight ());}}

The above ideas and part of the code is to draw on the Xiahanming great God's article, blog address: http://blog.csdn.net/xiaanming

If you have any questions, please leave a message, reproduced the source.

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.