Scrolling tricks and androidscrollview

Source: Internet
Author: User

Scrolling tricks and androidscrollview

I recently resigned from my job at home and had nothing to do with Android studio. I had various headaches when I first got started. Now I have adapted to it for a while and found that it is indeed more powerful than eclipse, so let's talk about the usage of scrollview today. I believe many people simply use scrollview as a rolling device. In fact, scrollview has many simple but cool usage, first look at the effect.


I believe many people will be familiar with it. This is the result of ScrollingTricks. Today we will analyze its working principles. After all, it will be better to understand its working principles.

1, STICKY

We know that the onScrollChanged method of scrollview can only be obtained by inheriting scrollview, so we can get the value we need through the callback of the interface.

Public class extends ScrollView {public Callbacks mCallbacks; public ObservableScrollView (Context context, AttributeSet attrs) {super (context, attrs);} public void setCallbacks (Callbacks callbacks) {this. mCallbacks = callbacks;} @ Override protected void onScrollChanged (int l, int t, int oldl, int oldt) {super. onScrollChanged (l, t, oldl, oldt); if (mCallbacks! = Null) {mCallbacks. onScrollchanged (t) ;}/ *** all vertical ranges represented by the vertical scroll bar. The default range is the drawing height of the current view. */Public int computeVerticalScrollRange () {return super. computeVerticalScrollRange ();} public interface Callbacks {public void onScrollchanged (int t); public void onTouchUp (); public void onTouchDown ();}}
Then we can reference the scrollview we write in the layout.

<com.example.apple.myapplication.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/scroll_view"    android:layout_width="match_parent"    android:layout_height="match_parent">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <View style="@style/Item.Top" />            <View android:id="@+id/placeholder"                android:layout_width="match_parent"                android:layout_height="@dimen/sticky_height" />            <View style="@style/Item.Bottom" />            <View style="@style/Item.Bottom.Alt" />            <View style="@style/Item.Bottom" />            <View style="@style/Item.Bottom.Alt" />            <View style="@style/Item.Bottom" />            <View style="@style/Item.Bottom.Alt" />        </LinearLayout>        <TextView android:id="@+id/sticky" style="@style/Item.Sticky" />    </FrameLayout></com.example.apple.myapplication.ObservableScrollView>

Note that FrameLayout must be used.

Public class StickyActivity extends Activity implements ObservableScrollView. callbacks {private TextView txtContent; private ObservableScrollView observableScrollView; private View mPlaceholderView; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_sticky); mPlaceholderView = (View) findViewById (R. id. placeholder); txtContent = (TextView) findViewById (R. id. sticky); txtContent. setText ("StickyActivity"); observableScrollView = (ObservableScrollView) findViewById (R. id. scroll_view); observableScrollView. setCallbacks (this);/*** we can get the view only when the layout is completely drawn. getTop () and so on */observableScrollView. getViewTreeObserver (). addOnGlobalLayoutListener (new ViewTreeObserver. onGlobalLayoutListener () {@ Override public void onGlobalLayout () {onScrollchanged (observableScrollView. getScrollY () ;}}) ;}@ Override public void onScrollchanged (int t) {int translation = Math. max (t, mPlaceholderView. getTop (); txtContent. setTranslationY (translation) ;}@ Override public void onTouchUp () {}@ Override public void onTouchDown (){}}

It's very easy, isn't it? But here I want to explain to you, getViewTreeObserver, because we want to get the view. getTop value, but we cannot get it in oncreate. the return value is always 0, so we need to add the overall layout listener.

The next onScrollchanged method is explained in the previous figure.


Why is my eyes full of tears because I have no painting talent at all ..

In this way, let's take a look at Math. max (t, gettop); is the constant comparison. When t is greater than gettop, we can think that it slipped to the top. Next, because t is increasing, our setTranslationY is also increasing, so our textview stops at the top.


2, QUICKRETURN


The result is that, no matter how far we slide, our textview can be displayed as long as we slide down. It is not difficult to analyze the code.


/*** Slide up status */private static final int STATE_ONSCREEN = 0;/*** slide up to completely hide mPlaceholderView */private static final int STATE_OFFSCREEN = 1; /*** when completely covered, the downloading status */private static final int STATE_RETURING = 2; private int mState = STATE_ONSCREEN;/*** height */private int mViewHeight; private int minRaw;

The others are similar. We can see that three States are added and a minraw value is used to determine and confirm the position.

   @Override    public void onScrollchanged(int t) {        int raw = mPlaceholderView.getTop() - t;        int translationY = 0;        switch (mState) {            case STATE_ONSCREEN:               // Log.d("TAG","STATE_ONSCREEN");                if (raw < -mViewHeight) {                    mState = STATE_OFFSCREEN;                    minRaw = raw;                }                translationY = raw;                break;            case STATE_OFFSCREEN:               // Log.d("TAG","STATE_OFFSCREEN");                if (raw<=minRaw){                    minRaw = raw;                }                else{                     mState = STATE_RETURING;                }                translationY = raw;                break;            case STATE_RETURING:                translationY = (raw - minRaw) - mViewHeight;                Log.d("TAG","translationY:"+translationY);                if (translationY > 0) {                    translationY = 0;                    minRaw = raw - mViewHeight;                }                if (raw > 0) {                    mState = STATE_ONSCREEN;                    translationY = raw;                }                if (translationY < -mViewHeight) {                    mState = STATE_OFFSCREEN;                    minRaw = raw;                }                break;        }        txtContent.setTranslationY(translationY+t);    }

This time, there may be more code in onscrollchange, but there is nothing to understand.

First, raw = getTop ()-t;

If the space capability is strong, you may have made up the screen by yourself. Here we will analyze t as the change value on the Y axis, and gettop as the value from the top, in this case, the current position is obtained.

STATE_ONSCREEN: slide upwards when raw <-ViewHeight indicates that our text is completely invisible on the screen, and the status changes to STATE_OFFSCREEN.

STATE_OFFSCREEN: At this time, we can no longer see text, so we only need to slide down when minraw> raw = true; then it is converted to STATE_RETURING.


STATE_RETURING: Since we slide down, we need to calculate the sliding distance and the text display part, so translationY = (raw-minRaw)-mViewHeight;


In this case, there should be nothing difficult to understand. Finally, let's talk about the fact that android studio is really cool and cool, so it's really useful when it's easy to use, and when it's hard to use it, people want to hit the computer !!


Project source code





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.