Haven't written a blog for a long time, today nothing more than anger.
As the title today we will imitate Sohu news, first.
After the last blog, I also get inspiration from here http://blog.csdn.net/qibin0506/article/details/42046559
1, and then we'll analyze the effect.
The following content should be used Viewpager, the head of the tab we use a linearlayout+horizontalscrollview can also be achieved, scrolling effect we can use the Scrollto we learned, the general idea of clear, Let's start the code!
2, Custom LinearLayout
We can observe that the red rectangle should be linearlayout with the whole, so we can customize the LinearLayout to draw the rectangle, and the scroll is to change the position of the rectangle.
<span style= "FONT-SIZE:14PX;" >public class Viewpagerindicator extends LinearLayout {/** * brush, bottom rectangle */private paint paint; /** * Child View number */private int mCount; private int mtop; private int mwidth; private int mleft; private int mheight = 5; Public Ontextclick Ontextclick; Public Viewpagerindicator (Context context) {super (context); } public Viewpagerindicator (context context, AttributeSet Attrs) {Super (context, attrs); SetBackgroundColor (color.transparent); Paint = new paint (); Paint.setcolor (Getresources (). GetColor (r.color.red)); Paint.setantialias (TRUE); } public interface ontextclick{public void Textviewclick (int position); } public void Setontextclick (Ontextclick ontextclick) {this.ontextclick = Ontextclick; } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); Overall height mtop = getmeasuredheight (); Overall width int wide = Getmeasuredwidth (); Plus indicator height int height = mtop + mheight; The width of the indicator mwidth = Width/mcount; Setmeasureddimension (width, height); }</span>
in Onmeasure we get the values we want for the location distance, and then we can draw the desired rectangle in the OnDraw.
<span style= "FONT-SIZE:14PX;" >/** * Draw indicator * @param canvas * /protected void OnDraw (canvas canvas) { rect rect = new Rect (Mleft, Mtop, Mleft + mwidth, mtop + mheight); Canvas.drawrect (rect, paint); } </span>
Yes, it's just two lines of code, and then you can follow the scrolling effect by constantly changing the left.
<span style= "FONT-SIZE:14PX;" > /** * scrolling effect * @param position * @param positionoffset * /public void scrollTo (int Position, float positionoffset) { mleft = (int) ((position + positionoffset) * mwidth); Postinvalidate (); } </span>
the two parameters of the method can be obtained through the Viewpager callback method onpagescrolled, position is the current tab, Positionoffset corresponds to the percentage of sliding 0.0~0.0 between.
3, using a custom control
OK, the general idea has been cleared, then we can use their own indicator.
<span style= "FONT-SIZE:14PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "Topnews.inhome.wrh.fragment.NewsFragment" > do not be more like a dog TextView scare, in fact, are pasted copy up to change a text on the line, we can also in their own linearlayout through TextView t = new TextView (), and then add in, Both of these methods are possible. But obviously we don't want to give so many textview the ID and then get its instance setonclick, so we can set it directly in the custom ViewGroup.
<span style= "FONT-SIZE:14PX;" > @Override protected void Onfinishinflate () { super.onfinishinflate (); MCount = Getchildcount (); /** * Monitor Click event, callback interface * /for (int i = 0; i<mcount;i++) { View view = Getchildat (i); if (view instanceof TextView) { final int finali = i; ((TextView) view). Setonclicklistener (New Onclicklistener () { @Override public void OnClick (view v) { if (Ontextclick! = null) { ontextclick.textviewclick (Finali);}} ); }}} </span>
through the callback interface we can easily get the click on which page tab, and then we take a look at how to use
<span style= "FONT-SIZE:14PX;" >public class Mainactivity extends Activity implements Viewpager.onpagechangelistener, viewpagerindicator.ontextclick{ private Viewpager Viewpager; private int lastx = 0; private Viewpagerindicator indicator; Private Horizontalscrollview Horizontalscrollview; protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initviews (); } private void Initviews () { Viewpager = (viewpager) Findviewbyid (R.id.viewpager); Indicator = (viewpagerindicator) Findviewbyid (r.id.indicator); Indicator.setontextclick (this); Horizontalscrollview = (Horizontalscrollview) Findviewbyid (R.id.horizon); Viewpager.setadapter (New Myadapter ()); Viewpager.setonpagechangelistener (this); } </span>
some of the necessary instantiation.
<span style= "FONT-SIZE:14PX;" > /** * Only when the second item is scrolled from the first item or the first item is scrolled from the second item, the overall fixed indicator scrolls alone, and the rest is the entire layout scroll * @param position * @param Positionoffset * @param positionoffsetpixels * /public void onpagescrolled (int position, float Positionoffset, int positionoffsetpixels) { if (! ( Position = = 0 && positionoffset-lastx >= 0) | | ! (Position = = 1 && positionoffset-lastx <= 0)) { Horizontalscrollview.scrollto ((int) ((positionoffset+position-1) *indicator.getindicatorwidth ()), 0); } Indicator.scrollto (position, positionoffset); LASTX = (int) positionoffset; } @Override public void onpageselected (int position) { indicator.resettextviewcolor (); Indicator.setfocustextview (position); } @Override public void Onpagescrollstatechanged (int.) { }</span>
by observing Sohu News we can see that when sliding from the first page to the second page, only indicator scrolling, and then all the overall scrolling, so we have to make a good judgment, the overall scrolling time indicator is equivalent to stay in the display of the second tab, Now let's look at the actual effect.
The effect is still wide, ka ka.
Project Source
Android mimics Sohu News's Viewpagerindicator