Recently resigned at home Idle to get up the Android studio, just start with a variety of headaches, now adapt to a time to find that it is really more powerful than eclipse, then we say today scrollview some usage, Believe that many people use ScrollView when it is just simple to use it as a rolling device, in fact, ScrollView have a lot of simple but cool usage, first look at an effect.
Believe that many people will feel familiar, this is the effect of scrollingtricks, we are today to analyze its working principle, after all, from the principle of understanding the effect will be better.
1,sticky
We know that the Onscrollchanged method of ScrollView can only be obtained by inheriting ScrollView, then we will take the value we need by the callback of the interface.
public class Observablescrollview 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, old t); 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 quote our own ScrollView 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:la yout_height= "wrap_content" android:orientation= "vertical" > <view style= "@style/item.top"/> <view android:id= "@+id/placeholder" android:layout_width= "Match_parent" Andro id:layout_height= "@dimen/sticky_height"/> <view style= "@style/item.bottom"/> <view sty le= "@style/item.bottom.alt"/> <view style= "@style/item.bottom"/> <view style= "@style/I Tem. 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.appl E.myapplication.observablescrollview>
Note that we must use framelayout inside.
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 view.gettop () etc when the layout is fully drawn ()/Observablescrollview.getviewtreeobserver (). Addongloballayo Utlistener (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 () {}}
very simple not, but here is still to explain to you, getviewtreeobserver, because we want to get to the value of view.gettop, but we get in the oncreate inside obtained, the return value is always 0, So we're going to add overall layout monitoring.
The next onscrollchanged method we'll explain in the last figure
Why my eyes are full of tears, because I have no talent for painting.
So we intuitively see, Math.max (T,gettop), is the constant comparison, when T is greater than gettop, we can think of slipping the top, and then because T is still increasing our settranslationy is also increasing, So the effect is that our textview stop at the top.
2,quickreturn
The effect is that no matter how far we slide, as long as we swipe down, our TextView can be displayed, and we'll go on to analyze the code, but it's not hard.
/** * on slide state */ private static final int state_onscreen = 0; /** * Swipe up to completely cover Mplaceholderview * / private static final int state_offscreen = 1; /** * Full cover, slide state */ private static final int state_returing = 2; private int mstate = State_onscreen; /** * Height * */ private int mviewheight; private int minraw;
The other is basically similar, we can see the new three states, and a minraw, which is used to make judgments and confirm the location of the
@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 the code inside the Onscrollchange may be a little more, but as long as the understanding is actually nothing.
The first is raw = GetTop ()-t;
The space capacity of the potential has been self-made out of the picture, we here to analyze the T is the value of the change on the Y axis, GetTop is the value of the distance from the top, then we subtract from this one is not the current position.
State_onscreen: Swipe up when raw <-viewheight shows that our text is completely invisible on the screen, and the state turns into State_offscreen.
State_offscreen: We can't see the text at this point, so we just swipe down Minraw>raw = true, and it turns into state_returing.
State_returing: Because we glide, so we have to figure out the sliding distance and the text display part, so translationy = (raw-minraw)-mviewheight;
This should not be difficult to understand, and finally said Android studio Real let people ice fire two heavy days, easy to use, when it is difficult to make people want to smash the computer!!
Project Source
Android ScrollView Magical (scrolling tricks detailed)