android change webview when change a tabstrip -
i wonder if can me work, want change webview url every time change tab in tabstrip
i new developer kind of great.
the main code of pager , tabs
import android.graphics.color; import android.graphics.drawable.colordrawable; import android.graphics.drawable.drawable; import android.graphics.drawable.layerdrawable; import android.graphics.drawable.transitiondrawable; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.support.v7.app.actionbaractivity; import android.support.v7.widget.toolbar; import android.util.typedvalue; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; import com.astuetz.pagerslidingtabstrip; import com.readystatesoftware.systembartint.systembartintmanager; import butterknife.butterknife; import butterknife.injectview; public class mainactivity extends actionbaractivity { @injectview(r.id.toolbar) toolbar toolbar; @injectview(r.id.tabs) pagerslidingtabstrip tabs; @injectview(r.id.pager) viewpager pager; private mypageradapter adapter; private drawable oldbackground = null; private int currentcolor; private systembartintmanager mtintmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.inject(this); setsupportactionbar(toolbar); // create our manager instance after content view set mtintmanager = new systembartintmanager(this); // enable status bar tint mtintmanager.setstatusbartintenabled(true); adapter = new mypageradapter(getsupportfragmentmanager()); pager.setadapter(adapter); tabs.setviewpager(pager); final int pagemargin = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, 4, getresources() .getdisplaymetrics()); pager.setpagemargin(pagemargin); pager.setcurrentitem(1); changecolor(getresources().getcolor(r.color.green)); tabs.setontabreselectedlistener(new pagerslidingtabstrip.ontabreselectedlistener() { @override public void ontabreselected(int position) { toast.maketext(mainactivity.this, "tab reselected: " + position, toast.length_short).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.action_contact: quickcontactfragment.newinstance().show(getsupportfragmentmanager(), "quickcontactfragment"); return true; } return super.onoptionsitemselected(item); } private void changecolor(int newcolor) { tabs.setbackgroundcolor(newcolor); mtintmanager.settintcolor(newcolor); // change actionbar color if actionbar available drawable colordrawable = new colordrawable(newcolor); drawable bottomdrawable = new colordrawable(getresources().getcolor(android.r.color.transparent)); layerdrawable ld = new layerdrawable(new drawable[]{colordrawable, bottomdrawable}); if (oldbackground == null) { getsupportactionbar().setbackgrounddrawable(ld); } else { transitiondrawable td = new transitiondrawable(new drawable[]{oldbackground, ld}); getsupportactionbar().setbackgrounddrawable(td); td.starttransition(200); } oldbackground = ld; currentcolor = newcolor; } public void oncolorclicked(view v) { int color = color.parsecolor(v.gettag().tostring()); changecolor(color); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("currentcolor", currentcolor); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); currentcolor = savedinstancestate.getint("currentcolor"); changecolor(currentcolor); } public class mypageradapter extends fragmentpageradapter { private final string[] titles = {"categories", "home", "top paid", "top free", "top grossing", "top new paid", "top new free", "trending"}; public mypageradapter(fragmentmanager fm) { super(fm); } @override public charsequence getpagetitle(int position) { return titles[position]; } @override public int getcount() { return titles.length; } @override public fragment getitem(int position) { return superawesomecardfragment.newinstance(position); } }
}
and second code of textview want change webview.
import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.view.viewcompat; import android.util.typedvalue; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.framelayout; import android.widget.framelayout.layoutparams; import android.widget.textview; import butterknife.butterknife; import butterknife.injectview; public class superawesomecardfragment extends fragment { private static final string arg_position = "position"; @injectview(r.id.textview) textview textview; private int position; public static superawesomecardfragment newinstance(int position) { superawesomecardfragment f = new superawesomecardfragment(); bundle b = new bundle(); b.putint(arg_position, position); f.setarguments(b); return f; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); position = getarguments().getint(arg_position); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_card,container,false); butterknife.inject(this, rootview); viewcompat.setelevation(rootview,50); textview.settext("card "+position); return rootview; }
}
that's now, layouts files less important, deal later.
you should setonpagechangelistener
, use webview.loadurl(url1);
tabsstrip.setonpagechangelistener(new onpagechangelistener() { // method invoked when new page becomes selected. @override public void onpageselected(int position) { switch(position) { case 0: webview.loadurl(url1); break; case 1: webview.loadurl(url2); break; case 2: webview.loadurl(url3); break; } } // method invoked when current page scrolled @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { // code goes here } // called when scroll state changes: // scroll_state_idle, scroll_state_dragging, scroll_state_settling @override public void onpagescrollstatechanged(int state) { // code goes here } });
Comments
Post a Comment