change viewpage swipe direction in RTL layer in android -
i try use rtl layerdirection in activity viewpager , tablayout.but in rtl layer viewpager swipe direction reversed , set currentitem viewpager , tablayout changed.
i try after , before setupwithviewpager dose not work.
any idea..!!
i try :
mviewpager = (viewpager) findviewbyid(r.id.mviewpager ); mviewpager.setadapter(adapter); mviewpager.setcurrentitem(mviewpager.getadapter().getcount() - 1,false); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager);
and :
mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); mviewpager.setcurrentitem(mviewpager.getadapter().getcount() - 1,false);
you can create custom pager adapter:
public class custom_pageradapter extends fragmentpageradapter{ /* * attributes: * * class attributes * * */ private arraylist<object> itemlist; /* * autoruns: * * these methods called @ startup * * */ ///////////// //////////////////constructor: ////////////// public custom_pageradapter(fragmentmanager fm){ super(fm); itemlist = new arraylist<>(); } /* * setters: * * sets class attributes * * */ ///////////// //////////////////additem: ////////////// public void additem(object item){itemlist.add(item);} /* * getters: * * gets class attributes * * */ ///////////// //////////////////getitem: ////////////// @override public fragment getitem(int position){ return (fragment) itemlist.get(getdirectionalpagerposition(position)); } ///////////// //////////////////getcount: ////////////// @override public int getcount(){return itemlist.size();} /* * other functions: * * class other functions * * */ ///////////// //////////////////getdirectionalpagerposition: //calculates pager position considering app direction public int getdirectionalpagerposition(int virtualposition){ int actualposition; if(core.getdirection() == direction.ltr)//make own if actualposition = virtualposition; else actualposition = getcount()-1-virtualposition; return actualposition; } ///////////// //////////////////getdirectionaltabposition: //calculates tab position considering app direction public int getdirectionaltabposition(int virtualposition){ int actualposition; if(core.getdirection() == direction.ltr) actualposition = virtualposition; else{ actualposition = getcount()-2-virtualposition; if(actualposition < 0) actualposition = 0; if(virtualposition > getcount()-2) actualposition = getcount()-1; } return actualposition; } ///////////// //////////////////getdirectionaltabpositionoffset: //calculates tab position offset considering app direction public float getdirectionaltabpositionoffset(float virtualpositionoffset){ float actualpositionoffset; if(core.getdirection() == direction.ltr) actualpositionoffset = virtualpositionoffset; else actualpositionoffset = 1-virtualpositionoffset; return actualpositionoffset; }
}
and use it:
/* * initializers: * * initialize components * * */ ///////////// //////////////////inittabbar: ////////////// private void inittabbar(){ main_tablyt = (customtablayout) findviewbyid(r.id.main_tablyt); /** count of tabs must equal count of pages in {@link #initpager()} */ main_tablyt.addtab(main_tablyt.newtab().settext("tab 1")); main_tablyt.addtab(main_tablyt.newtab().settext("tab 2")); main_tablyt.addtab(main_tablyt.newtab().settext("tab 3")); addtabbarontabselectedlistener(); } ///////////// //////////////////initpager: ////////////// private void initpager(){ main_pgradp = new custom_pageradapter(getsupportfragmentmanager()); /** count of pages must equal count of tabs in {@link #inittabbar()} */ main_pgradp.additem(new view1()); main_pgradp.additem(new view2()); main_pgradp.additem(new view3()); main_viwpgr = (viewpager) findviewbyid(r.id.main_pager); main_viwpgr.setadapter(main_pgradp); main_viwpgr.setcurrentitem(main_pgradp.getdirectionalpagerposition(0));//first page considering app direction addpageronpagechangelistener(); } /* * listeners: * * these methods called after user action * * */ ///////////// //////////////////addtabbarontabselectedlistener: ////////////// public void addtabbarontabselectedlistener(){ main_tablyt.addontabselectedlistener(new tablayout.ontabselectedlistener(){ @override public void ontabselected(tab tab){//selects pager item considering app direction main_viwpgr.setcurrentitem(main_pgradp.getdirectionalpagerposition(tab.getposition())); } @override public void ontabunselected(tab tab){} @override public void ontabreselected(tab tab){} }); } ///////////// //////////////////addpageronpagechangelistener: ////////////// private void addpageronpagechangelistener(){ main_viwpgr.addonpagechangelistener(new viewpager.onpagechangelistener(){ @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels){//sets tab bar scroll position considering app direction main_tablyt.setscrollposition(main_pgradp.getdirectionaltabposition(position), main_pgradp.getdirectionaltabpositionoffset(positionoffset), false); } @override public void onpageselected(int position){ tab tab = main_tablyt.gettabat(main_pgradp.getdirectionalpagerposition(position)); if(tab != null) tab.select(); } @override public void onpagescrollstatechanged(int state){} }); }
Comments
Post a Comment