Android Faux today headline hand interface

Source: Internet
Author: User

public class Myindicator extends Horizontalscrollview implements Viewpager.onpagechangelistener {private Viewpager MVi    Ewpager;    Private LinearLayout mylinearlayout; /** * We implement the Viewpager.onpagechangelistener interface, because we want to listen to the viewpager of the sliding page behavior, so as to change the status of the navigation bar * So we can also see that myindicator holding Viewpager     What if we still want to listen to Viewpager?    * Here I have provided an interface for Myindicator, like the Onpagechangelistener method called **/Myonpagechangelistener Mlistener; Private interface Myonpagechangelistener {void onpagescrolled (int position, float positionoffset, int positionoffs        Etpixels);        void onpageselected (int position);    void onpagescrollstatechanged (int state);    } private int oldselected; In order for TextView to click, I set the Onclicklistener for each textview,//is to get the target title bar index, and then call Setcurrentitem () on it, so that the effect of click-Sliding,    Click on the title bar, Viewpager will also follow the page.            Private final Onclicklistener Mtabclicklistener = new Onclicklistener () {public void OnClick (view view) {            Mytabview Tabview = (mytabview) view; OldSelected = Mviewpager.getcurrentitem ();            Final int newselected = Tabview.index;        Setcurrentitem (newselected);    }    };        Public Myindicator (Context context) {super (context);    Init (context);        } public Myindicator (context context, AttributeSet Attrs) {Super (context, attrs);    Init (context);        } public Myindicator (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    Init (context);        } private void Init (Context mcontext) {sethorizontalscrollbarenabled (false);//hidden scroll bar//Add LinearLayout        Mylinearlayout = new LinearLayout (mcontext);        Mylinearlayout.setorientation (linearlayout.horizontal);    AddView (Mylinearlayout, New Viewgroup.layoutparams (Layoutparams.wrap_content, layoutparams.match_parent));    } public void Setviewpager (Viewpager viewpager) {setviewpager (Viewpager, 0); } @SuppressWarnings ("deprecation") public void SETVIEWPAger (Viewpager viewpager, int initpos) {if (Mviewpager = = Viewpager) {return;        } if (Mviewpager! = null) {Mviewpager.setonpagechangelistener (null);        } final Pageradapter adapter = Viewpager.getadapter ();        if (adapter = = null) {throw new IllegalStateException ("Viewpager does not has adapter instance.");        } Mviewpager = Viewpager;        Viewpager.setonpagechangelistener (this);        Notifydatasetchanged ();    Setcurrentitem (Initpos);        } private void Notifydatasetchanged () {mylinearlayout.removeallviews ();        Pageradapter madapter = Mviewpager.getadapter ();        int count = Madapter.getcount ();        for (int i = 0; i < count; i++) {AddTab (I, Madapter.getpagetitle (i));    } requestlayout (); The/** * code adds the top textview * * @param index * @param text */private void AddTab (int index, CHARSEQ Uence text) {Mytabview Tabview = new Mytabview (GetContext ());        Tabview.index = index;        Tabview.setfocusable (TRUE);        Tabview.setonclicklistener (Mtabclicklistener);        Tabview.settext (text);        Tabview.settextsize (22);        Tabview.settextcolor (GetContext (). Getresources (). GetColor (R.color.colorwhite));        Tabview.setpadding (20, 0, 20, 0);        Tabview.settextalignment (View.text_alignment_center);        Pageradapter madapter = Mviewpager.getadapter ();        int screenwidth = Getresources (). Getdisplaymetrics (). Widthpixels;        if (Madapter.getcount () <= 4) {tabview.setwidth (Screenwidth/madapter.getcount ());        } else {tabview.setwidth (SCREENWIDTH/4);    } mylinearlayout.addview (Tabview); }/** * Selected Animation * * @param view */@TargetApi (build.version_codes. Honeycomb) @SuppressLint ("Newapi") private void animation (view view) {Objectanimator ScaleX = Objectanimator        . Offloat (View, "ScaleX", 1f); OBjectanimator ScaleY = objectanimator.offloat (view, "ScaleY", 1f);        Objectanimator fade = objectanimator.offloat (view, "Alpha", 1f);        Animatorset animset = new Animatorset ();        Animset.play (ScaleX). with (ScaleY). with (fade);        Animset.setduration (500);    Animset.start (); }/** * Not selected Animation * * @param view */private void Animation2 (view view) {Objectanimator ScaleX        = Objectanimator.offloat (view, "ScaleX", 0.8f);        Objectanimator ScaleY = objectanimator.offloat (view, "ScaleY", 0.8f);        Objectanimator fade = objectanimator.offloat (view, "Alpha", 0.5f);        Animatorset animset = new Animatorset ();        Animset.play (ScaleX). with (ScaleY). with (fade);        Animset.setduration (500);    Animset.start (); } public void Setcurrentitem (int item) {if (Mviewpager = = null) {throw new IllegalStateException ("V        Iewpager have not been bound. ");        int mselectedtabindex = Item; Mviewpager.Setcurrentitem (item);        Final int tabcount = Mylinearlayout.getchildcount ();            for (int i = 0; i < Tabcount; i++) {//Traverse title, change the selected background to final View child = Mylinearlayout.getchildat (i);            Final Boolean isSelected = (i = = Item);            Child.setselected (isSelected);                if (isSelected) {animation (child);                Animatetotab (item);//Animation effect} else {animation2 (child);            Child.setbackgroundcolor (color.transparent);    }}} private Runnable mtabselector;        private void Animatetotab (final int position) {final View Tabview = mylinearlayout.getchildat (position);        if (mtabselector! = null) {removecallbacks (mtabselector); } mtabselector = new Runnable () {public void run () {final int scrollpos = Tabview.getle   FT ()-(GetWidth ()-tabview.getwidth ())/2;//calculates the position to slide to Smoothscrollto (scrollpos, 0);             Mtabselector = null;        }        };    Post (Mtabselector);    Perform animation on the main thread} public void Setmyonpagechangelistener (Myonpagechangelistener listener) {Mlistener = listener; } @Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {if (    Mlistener = null) mlistener.onpagescrolled (position, Positionoffset, positionoffsetpixels);        } @Override public void onpageselected (int position) {Setcurrentitem (position);    if (Mlistener! = null) mlistener.onpageselected (position); } @Override public void onpagescrollstatechanged (int state) {if (Mlistener! = null) mlistener.onpagescrollst    Atechanged (state); }}

Use:

<com.cqytjr.www.cheji.views.myindicator        android:id= "@+id/indicator"        android:layout_width= "Match_ Parent "        android:layout_height=" wrap_content "        android:background=" #c4c77f "/>    < Android.support.v4.view.ViewPager        android:id= "@+id/pager"        android:layout_width= "Match_parent        " android:layout_height= "0DP"        android:layout_weight= "1"/>

New Myfragmentmanager (Getsupportfragmentmanager (), content);         = (Viewpager) Findviewbyid (R.id.pager);         = (myindicator) Findviewbyid (r.id.indicator);        Pager.setadapter (adapter);        Indicator.setviewpager (pager);

Android Mimic today's headline hand interface

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.