java - set default tab of PagerSlidingTab library -


i'm using pagerslidingtab library
github.com/astuetz/pagerslidingtabstrip

want tabs start tab2 right, use this:

pager.setcurrentitem(2); 

its ok,but want tab text color change after selected changed library source code
refer : https://github.com/astuetz/pagerslidingtabstrip/pull/62/files?short_path=04c6e90
so library source [after change]:

package com.astuetz;          import android.annotation.suppresslint;         import android.content.context;         import android.content.res.typedarray;         import android.graphics.canvas;         import android.graphics.paint;         import android.graphics.paint.style;         import android.graphics.typeface;         import android.os.build;         import android.os.parcel;         import android.os.parcelable;         import android.support.v4.view.viewpager;         import android.support.v4.view.viewpager.onpagechangelistener;         import android.util.attributeset;         import android.util.displaymetrics;         import android.util.typedvalue;         import android.view.gravity;         import android.view.view;         import android.view.view.basesavedstate;         import android.view.view.onclicklistener;         import android.view.viewtreeobserver.ongloballayoutlistener;         import android.widget.horizontalscrollview;         import android.widget.imagebutton;         import android.widget.linearlayout;         import android.widget.textview;         import android.widget.framelayout.layoutparams;          import java.util.locale;          import com.astuetz.pagerslidingtabstrip.r;  public class pagerslidingtabstrip extends horizontalscrollview {      public interface icontabprovider {         public int getpageiconresid(int position);     }      // @formatter:off     private static final int[] attrs = new int[] {             android.r.attr.textsize,             android.r.attr.textcolor     };     // @formatter:on      private linearlayout.layoutparams defaulttablayoutparams;     private linearlayout.layoutparams expandedtablayoutparams;      private final pagelistener pagelistener = new pagelistener();     public onpagechangelistener delegatepagelistener;      private linearlayout tabscontainer;     private viewpager pager;      private int tabcount;      private int currentposition = 0;     private float currentpositionoffset = 0f;      private paint rectpaint;     private paint dividerpaint;      private int indicatorcolor = 0xff666666;     private int underlinecolor = 0x1a000000;     private int dividercolor = 0x1a000000;      private boolean shouldexpand = true;     private boolean textallcaps = true;      private int scrolloffset = 52;     private int indicatorheight = 8;     private int underlineheight = 2;     private int dividerpadding = 12;     private int tabpadding = 24;     private int dividerwidth = 1;      private int tabtextsize = 12;     private int tabtextcolor = 0xff666666;     private int tabdeactivatetextcolor = 0xbbbbbb;     private typeface tabtypeface = null;     private int tabtypefacestyle = typeface.normal;      private int lastscrollx = 0;      private int tabbackgroundresid = r.drawable.background_tab;     private int transparentcolorid = r.color.transparent;      private locale locale;     private boolean tabswitch;      public pagerslidingtabstrip(context context) {         this(context, null);      }      public pagerslidingtabstrip(context context, attributeset attrs) {         this(context, attrs, 0);     }      public pagerslidingtabstrip(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);          setfillviewport(true);         setwillnotdraw(false);          tabscontainer = new linearlayout(context);         tabscontainer.setorientation(linearlayout.horizontal);         tabscontainer.setlayoutparams(new layoutparams(layoutparams.match_parent, layoutparams.match_parent));         addview(tabscontainer);          displaymetrics dm = getresources().getdisplaymetrics();          scrolloffset = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, scrolloffset, dm);         indicatorheight = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, indicatorheight, dm);         underlineheight = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, underlineheight, dm);         dividerpadding = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dividerpadding, dm);         tabpadding = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, tabpadding, dm);         dividerwidth = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dividerwidth, dm);         tabtextsize = (int) typedvalue.applydimension(typedvalue.complex_unit_sp, tabtextsize, dm);          // system attrs (android:textsize , android:textcolor)          typedarray = context.obtainstyledattributes(attrs, attrs);          tabtextsize = a.getdimensionpixelsize(0, tabtextsize);         tabtextcolor = a.getcolor(1, tabtextcolor);           a.recycle();          // custom attrs          = context.obtainstyledattributes(attrs, r.styleable.pagerslidingtabstrip);          indicatorcolor = a.getcolor(r.styleable.pagerslidingtabstrip_pstsindicatorcolor, indicatorcolor);         underlinecolor = a.getcolor(r.styleable.pagerslidingtabstrip_pstsunderlinecolor, underlinecolor);         dividercolor = a.getcolor(r.styleable.pagerslidingtabstrip_pstsdividercolor, dividercolor);         indicatorheight = a.getdimensionpixelsize(r.styleable.pagerslidingtabstrip_pstsindicatorheight, indicatorheight);         underlineheight = a.getdimensionpixelsize(r.styleable.pagerslidingtabstrip_pstsunderlineheight, underlineheight);         dividerpadding = a.getdimensionpixelsize(r.styleable.pagerslidingtabstrip_pstsdividerpadding, dividerpadding);         tabpadding = a.getdimensionpixelsize(r.styleable.pagerslidingtabstrip_pststabpaddingleftright, tabpadding);         tabbackgroundresid = a.getresourceid(r.styleable.pagerslidingtabstrip_pststabbackground, tabbackgroundresid);         shouldexpand = a.getboolean(r.styleable.pagerslidingtabstrip_pstsshouldexpand, shouldexpand);         scrolloffset = a.getdimensionpixelsize(r.styleable.pagerslidingtabstrip_pstsscrolloffset, scrolloffset);         textallcaps = a.getboolean(r.styleable.pagerslidingtabstrip_pststextallcaps, textallcaps);         tabswitch = a.getboolean(r.styleable.pagerslidingtabstrip_pststabswitch, tabswitch);         tabtextcolor = a.getcolor(r.styleable.pagerslidingtabstrip_pstsactivatetextcolor, tabtextcolor);         tabdeactivatetextcolor = a.getcolor(r.styleable.pagerslidingtabstrip_pstsdeactivatetextcolor, tabdeactivatetextcolor);          a.recycle();         rectpaint = new paint();         rectpaint.setantialias(true);         rectpaint.setstyle(style.fill);          dividerpaint = new paint();         dividerpaint.setantialias(true);         dividerpaint.setstrokewidth(dividerwidth);          defaulttablayoutparams = new linearlayout.layoutparams(layoutparams.wrap_content, layoutparams.match_parent);         expandedtablayoutparams = new linearlayout.layoutparams(0, layoutparams.match_parent, 1.0f);          if (locale == null) {             locale = getresources().getconfiguration().locale;         }     }      public void setviewpager(viewpager pager) {         this.pager = pager;          if (pager.getadapter() == null) {             throw new illegalstateexception("viewpager not have adapter instance.");         }          pager.setonpagechangelistener(pagelistener);          notifydatasetchanged();     }      public void setonpagechangelistener(onpagechangelistener listener) {         this.delegatepagelistener = listener;     }      public void notifydatasetchanged() {          tabscontainer.removeallviews();          tabcount = pager.getadapter().getcount();          (int = 0; < tabcount; i++) {              if (pager.getadapter() instanceof icontabprovider) {                 addicontab(i, ((icontabprovider) pager.getadapter()).getpageiconresid(i));             } else {                 addtexttab(i, pager.getadapter().getpagetitle(i).tostring());             }          }          updatetabstyles();          getviewtreeobserver().addongloballayoutlistener(new ongloballayoutlistener() {              @suppresswarnings("deprecation")             @suppresslint("newapi")             @override             public void ongloballayout() {                  if (build.version.sdk_int < build.version_codes.jelly_bean) {                     getviewtreeobserver().removeglobalonlayoutlistener(this);                 } else {                     getviewtreeobserver().removeongloballayoutlistener(this);                 }                  currentposition = pager.getcurrentitem();                 scrolltochild(currentposition, 0);             }         });      }      private void addtexttab(final int position, string title) {         textview tab = new textview(getcontext());         tab.settext(title);         tab.setgravity(gravity.center);         tab.setsingleline();          addtab(position, tab);     }      private void addicontab(final int position, int resid) {          imagebutton tab = new imagebutton(getcontext());         tab.setimageresource(resid);          addtab(position, tab);      }      private void addtab(final int position, view tab) {         tab.setfocusable(true);         tab.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 pager.setcurrentitem(position);             }         });          tab.setpadding(tabpadding, 0, tabpadding, 0);         tabscontainer.addview(tab, position, shouldexpand ? expandedtablayoutparams : defaulttablayoutparams);     }      private void updatetabstyles() {          (int = 0; < tabcount; i++) {              view v = tabscontainer.getchildat(i);              v.setbackgroundresource(!tabswitch ? tabbackgroundresid : transparentcolorid);              if (v instanceof textview) {                  textview tab = (textview) v;                 tab.settextsize(typedvalue.complex_unit_px, tabtextsize);                 tab.settypeface(tabtypeface, tabtypefacestyle);                 tab.settextcolor(tabswitch && != 0 ? tabdeactivatetextcolor : tabtextcolor);                  // setallcaps() available api 14, upper case made manually if on                 // pre-ics-build                 if (textallcaps) {                     if (build.version.sdk_int >= build.version_codes.ice_cream_sandwich) {                         tab.setallcaps(true);                     } else {                         tab.settext(tab.gettext().tostring().touppercase(locale));                     }                  }              }else if (v instanceof imagebutton) {                 imagebutton tab = (imagebutton) v;                 tab.setselected(tabswitch && == 0 ? true : false);             }          }      }     private void updateactivatetab(final int position) {          (int = 0; < tabcount; i++) {              view v = tabscontainer.getchildat(i);              if (v instanceof textview) {                 textview tab = (textview) v;                 tab.settextcolor(position == ? tabtextcolor : tabdeactivatetextcolor);             } else {                 v.setselected(position == ? true : false);             }         }     }      private void scrolltochild(int position, int offset) {          if (tabcount == 0) {             return;         }          int newscrollx = tabscontainer.getchildat(position).getleft() + offset;          if (position > 0 || offset > 0) {             newscrollx -= scrolloffset;         }          if (newscrollx != lastscrollx) {             lastscrollx = newscrollx;             scrollto(newscrollx, 0);         }      }      @override     protected void ondraw(canvas canvas) {         super.ondraw(canvas);          if (isineditmode() || tabcount == 0) {             return;         }          final int height = getheight();          // draw indicator line          rectpaint.setcolor(indicatorcolor);          // default: line below current tab         view currenttab = tabscontainer.getchildat(currentposition);         float lineleft = currenttab.getleft();         float lineright = currenttab.getright();          // if there offset, start interpolating left , right coordinates between current , next tab         if (currentpositionoffset > 0f && currentposition < tabcount - 1) {              view nexttab = tabscontainer.getchildat(currentposition + 1);             final float nexttableft = nexttab.getleft();             final float nexttabright = nexttab.getright();              lineleft = (currentpositionoffset * nexttableft + (1f - currentpositionoffset) * lineleft);             lineright = (currentpositionoffset * nexttabright + (1f - currentpositionoffset) * lineright);         }          canvas.drawrect(lineleft, height - indicatorheight, lineright, height, rectpaint);          // draw underline          rectpaint.setcolor(underlinecolor);         canvas.drawrect(0, height - underlineheight, tabscontainer.getwidth(), height, rectpaint);          // draw divider          dividerpaint.setcolor(dividercolor);         (int = 0; < tabcount - 1; i++) {             view tab = tabscontainer.getchildat(i);             canvas.drawline(tab.getright(), dividerpadding, tab.getright(), height - dividerpadding, dividerpaint);         }     }      private class pagelistener implements onpagechangelistener {          @override         public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {              currentposition = position;             currentpositionoffset = positionoffset;              scrolltochild(position, (int) (positionoffset * tabscontainer.getchildat(position).getwidth()));              invalidate();              if (delegatepagelistener != null) {                 delegatepagelistener.onpagescrolled(position, positionoffset, positionoffsetpixels);             }         }          @override         public void onpagescrollstatechanged(int state) {             if (state == viewpager.scroll_state_idle) {                 scrolltochild(pager.getcurrentitem(), 0);             }              if (delegatepagelistener != null) {                 delegatepagelistener.onpagescrollstatechanged(state);             }         }          @override         public void onpageselected(int position) {             if (tabswitch) {                 updateactivatetab(position);             }             if (delegatepagelistener != null) {                 delegatepagelistener.onpageselected(position);             }         }      }      public void setindicatorcolor(int indicatorcolor) {         this.indicatorcolor = indicatorcolor;         invalidate();     }      public void setindicatorcolorresource(int resid) {         this.indicatorcolor = getresources().getcolor(resid);         invalidate();     }      public int getindicatorcolor() {         return this.indicatorcolor;     }      public void setindicatorheight(int indicatorlineheightpx) {         this.indicatorheight = indicatorlineheightpx;         invalidate();     }      public int getindicatorheight() {         return indicatorheight;     }      public void setunderlinecolor(int underlinecolor) {         this.underlinecolor = underlinecolor;         invalidate();     }      public void setunderlinecolorresource(int resid) {         this.underlinecolor = getresources().getcolor(resid);         invalidate();     }      public int getunderlinecolor() {         return underlinecolor;     }      public void setdividercolor(int dividercolor) {         this.dividercolor = dividercolor;         invalidate();     }      public void setdividercolorresource(int resid) {         this.dividercolor = getresources().getcolor(resid);         invalidate();     }      public int getdividercolor() {         return dividercolor;     }      public void setunderlineheight(int underlineheightpx) {         this.underlineheight = underlineheightpx;         invalidate();     }      public int getunderlineheight() {         return underlineheight;     }      public void setdividerpadding(int dividerpaddingpx) {         this.dividerpadding = dividerpaddingpx;         invalidate();     }      public int getdividerpadding() {         return dividerpadding;     }      public void setscrolloffset(int scrolloffsetpx) {         this.scrolloffset = scrolloffsetpx;         invalidate();     }      public int getscrolloffset() {         return scrolloffset;     }      public void setshouldexpand(boolean shouldexpand) {         this.shouldexpand = shouldexpand;         requestlayout();     }      public boolean getshouldexpand() {         return shouldexpand;     }      public boolean istextallcaps() {         return textallcaps;     }      public void setallcaps(boolean textallcaps) {         this.textallcaps = textallcaps;     }      public void settextsize(int textsizepx) {         this.tabtextsize = textsizepx;         updatetabstyles();     }      public int gettextsize() {         return tabtextsize;     }      public void settextcolor(int textcolor) {         this.tabtextcolor = textcolor;         updatetabstyles();     }      public void settextcolorresource(int resid) {         this.tabtextcolor = getresources().getcolor(resid);         updatetabstyles();     }      public int gettextcolor() {         return tabtextcolor;     }      public void settypeface(typeface typeface, int style) {         this.tabtypeface = typeface;         this.tabtypefacestyle = style;         updatetabstyles();     }      public void settabbackground(int resid) {         this.tabbackgroundresid = resid;     }      public int gettabbackground() {         return tabbackgroundresid;     }      public void settabpaddingleftright(int paddingpx) {         this.tabpadding = paddingpx;         updatetabstyles();     }      public int gettabpaddingleftright() {         return tabpadding;     }      public void settabswitch(boolean tabswitch) {         this.tabswitch = tabswitch;         updatetabstyles();     }      public void setactivatetextcolor(int activatetextcolor) {         this.tabtextcolor = activatetextcolor;         updatetabstyles();     }      public void setdeactivatetextcolor(int deactivatetextcolor) {         this.tabdeactivatetextcolor = deactivatetextcolor;         updatetabstyles();     }      @override     public void onrestoreinstancestate(parcelable state) {         savedstate savedstate = (savedstate) state;         super.onrestoreinstancestate(savedstate.getsuperstate());         currentposition = savedstate.currentposition;         requestlayout();     }      @override     public parcelable onsaveinstancestate() {         parcelable superstate = super.onsaveinstancestate();         savedstate savedstate = new savedstate(superstate);         savedstate.currentposition = currentposition;         return savedstate;     }      static class savedstate extends basesavedstate {         int currentposition;          public savedstate(parcelable superstate) {             super(superstate);         }          private savedstate(parcel in) {             super(in);             currentposition = in.readint();         }          @override         public void writetoparcel(parcel dest, int flags) {             super.writetoparcel(dest, flags);             dest.writeint(currentposition);         }          public static final parcelable.creator<savedstate> creator = new parcelable.creator<savedstate>() {             @override             public savedstate createfromparcel(parcel in) {                 return new savedstate(in);             }              @override             public savedstate[] newarray(int size) {                 return new savedstate[size];             }         };     }  } 

but problem when activity start,selected tab , default tab(highlight tab text color) different!
screen shot:
http://goo.gl/9tgho6

tnx...

you can achieve by:

private void updatetabstyles() {          (int = 0; < tabcount; i++) {              view v = tabscontainer.getchildat(i);              v.setbackgroundresource(!tabswitch ? tabbackgroundresid : transparentcolorid);              if (v instanceof textview) {                  textview tab = (textview) v;                 tab.settextsize(typedvalue.complex_unit_px, tabtextsize);                 tab.settypeface(tabtypeface, tabtypefacestyle);         //  tab.settextcolor(tabswitch && != 0 ? tabdeactivatetextcolor : tabtextcolor);                   if (textallcaps) {                     if (build.version.sdk_int >= build.version_codes.ice_cream_sandwich) {                         tab.setallcaps(true);                     } else {                         tab.settext(tab.gettext().tostring().touppercase(locale));                     }                  }              }else if (v instanceof imagebutton) {                 imagebutton tab = (imagebutton) v;                 tab.setselected(tabswitch && == 0 ? true : false);             }          }      } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -