Android scrollview slide onto a fixed control (Meituan group buying details UI) Perfect version, androidscrollview

Source: Internet
Author: User

Android scrollview slide onto a fixed control (Meituan group buying details UI) Perfect version, androidscrollview

In scrollview, sliding to fix a certain control (Meituan group buying details UI) describes how to use touchlistener to achieve a similar effect, but this method has a significant bug, when there are many contents, most people view the content in slide mode, rather than touch mode. This will cause the top slider to be delayed or delayed, here we will introduce a brand new method to achieve similar results, which can solve the above problems well.

Currently, there is no onscrolllistener in scrollview, so you need to implement it by yourself. First, re-write 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, Attri ButeSet attrs, int defStyle) {super (context, attrs, defStyle);}/*** set the rolling interface * @ param onScrollListener */public void setOnScrollListener (OnScrollListener onScrollListener) {this. onScrollListener = onScrollListener;} @ Overrideprotected void onScrollChanged (int l, int t, int oldl, int oldt) {// The system calls super in real time when the sliding changes. onScrollChanged (l, t, oldl, oldt); if (onScrollListener! = Null) {onScrollListener. onScroll (t) ;}}/***** rolling callback interface **/public interface OnScrollListener {/*** callback method, returns the Y distance of MyScrollView sliding * @ param scrollY *, */public void onScroll (int scrollY );}}

Then the following code 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 that it inherits the custom listener/*** custom MyScrollView */private MyScrollView myScrollView;/*** in MyS Purchase layout in crollView */private LinearLayout mBuyLayout;/*** purchase layout at the top */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); // slide Dynamic purchase layout mTopBuyLayout = (LinearLayout) findViewById (R. id. top_buy_layout); // The purchase layout displayed on the top is myScrollView. setOnScrollListener (this); // The findViewById (R. id. parent_layout ). getViewTreeObserver (). addOnGlobalLayoutListener (new OnGlobalLayoutListener () {// @ Overridepublic void onGlobalLayout () {// this step is very important, so that the above purchase layout and the following purchase layout coincide with onScroll (myScrollView. getScrollY () ;}}) ;}@ Overridepublic void onScroll (int ScrollY) {// This is the callback interface call function. The layout function is the view re-painting method, which is used when the current purchase layout has not slipped to the top, the two la s are drawn together, and the current purchase layout slides out of the screen, so that the purchase layout on the top does not move on the top to achieve similar results. 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 learn from the articles of the god Xia an Ming, blog address: http://blog.csdn.net/xiaanming

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


Android sets the height of the control (ScrollView) in the code.

Generally, the View is as convenient as you said. If that doesn't work, the Scrollview may be more special. I usually make the ScrollView wrap_content, and the layout in the ScrollView should not be wrap_content, for example, a RelaytiveLayout is nested in it, and the height of the RelaytiveLayout is written to death. Set the RelaytiveLayout height in your code and try to meet your needs.

How can I respond to the buttons added in scrollview due to the UI control problem?

What does button mean? I don't know how to display this button, or how to display the current date when I click a button?

Related Article

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.