android - Using a generic class for multiple objects (actionBar tabs) -
currently use abs, actionbar tabs , tabsadapter/viewpager make nice tab layout app. have 5+ category tabs top - user able add new categories (i'll set later) well. so, @ moment, have main sherlockfragmentactivity many sherlockfragment category files. in oncreate main sfa, build actionbar , add tabs, so:
mtabsadapter.addtab(bar.newtab().settext(r.string.login), loginfragment.class, null); mtabsadapter.addtab(bar.newtab().settext("geographics"), geofragment.class, null); mtabsadapter.addtab(bar.newtab().settext(r.string.economics), econfragment.class, null); mtabsadapter.addtab(bar.newtab().settext(r.string.elections), electionsfragment.class, null); what instead, create new solution, categoryfragment rather using of specific elections, geo, econ etc. can imagine solution? ideally, pass string tab gets added, categoryfragment can inflate based on string. solution because code redundant in multiple classes, when class load stuff sql db online, getting data own category.
here tabsadapter class:
public class tabsadapter extends fragmentpageradapter implements actionbar.tablistener, viewpager.onpagechangelistener { private final context mcontext; private polling activity; private final actionbar mactionbar; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>(); final class tabinfo { private final class<?> clss; private final bundle args; //private final string title; //this string implemented part of attempt! tabinfo(class<?> _class, bundle _args, string _title) { clss = _class; args = _args; title = _title; } } /*constructor method adds tabsadapter each tab created. * adds viewpager each tab user can swipe change tabs. */ public tabsadapter(sherlockfragmentactivity activity, viewpager pager) { super(activity.getsupportfragmentmanager()); mcontext = activity; this.activity = (polling) activity; mactionbar = activity.getsupportactionbar(); mviewpager = pager; mviewpager.setadapter(this); mviewpager.setonpagechangelistener(this); } /*this method i've been trying use solve problem, it's not cutting it!*/ public void buildtabs() throws classnotfoundexception { string [] tabs = {"econ", "elections", "geo", "politics", "science", "finance", "religion", "military", "international" }; final string resource = "r.string."; mtabsadapter.addtab(bar.newtab().settext("login"), loginfragment.class, null, "login"); (int j = 0; j < tabs.length; j++) { string res = resource + tabs[j]; string clas = tabs[j] + "fragment"; string total = "com.davekelley.polling." + clas; mtabsadapter.addtab(bar.newtab().settext(tabs[j]), categoryfragment.class, null, tabs[j]); } } /*a simple method sets tabinfo each tab tabsadapter * knows class tab being added belonds to. updates * ui interface when each tab added. */ public void addtab(actionbar.tab tab, class<?> clss, bundle args, string title) { tabinfo info = new tabinfo(clss, args, title); tab.settag(info); tab.settablistener(this); mtabs.add(info); mactionbar.addtab(tab); notifydatasetchanged(); } public int getcount() { return mtabs.size(); } /*a method used in other classes allow each tab fragment * access inherited methods mother-class, in case, sherlockfragment */ public int getposition(sherlockfragment fragment) { (int j = 1; j < mtabs.size(); j++) { tabinfo info = (tabinfo) mactionbar.gettabat(j).gettag(); if (info.title.matches(mtabs.get(j).title)) { return j; } } return -1; } public sherlockfragment getitem(int position) { tabinfo info = mtabs.get(position); return (sherlockfragment)fragment.instantiate(mcontext, info.clss.getname(), info.args); } /*this method reads user's selection new tab , sets tab * new current focus.*/ public void onpageselected(int position) { mactionbar.setselectednavigationitem(position); selectinspinnerifpresent(position, true); } private void selectinspinnerifpresent(int position, boolean animate) { try { view actionbarview = findviewbyid(r.id.abs__action_bar); if (actionbarview == null) { int id = getresources().getidentifier("action_bar", "id", "android"); actionbarview = findviewbyid(id); } class<?> actionbarviewclass = actionbarview.getclass(); field mtabscrollviewfield = actionbarviewclass.getdeclaredfield("mtabscrollview"); mtabscrollviewfield.setaccessible(true); object mtabscrollview = mtabscrollviewfield.get(actionbarview); if (mtabscrollview == null) { return; } field mtabspinnerfield = mtabscrollview.getclass().getdeclaredfield("mtabspinner"); mtabspinnerfield.setaccessible(true); object mtabspinner = mtabspinnerfield.get(mtabscrollview); if (mtabspinner == null) { return; } method setselectionmethod = mtabspinner.getclass().getsuperclass().getdeclaredmethod("setselection", integer.type, boolean.type); setselectionmethod.invoke(mtabspinner, position, animate); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } catch (nosuchfieldexception e) { e.printstacktrace(); } catch (nosuchmethodexception e) { e.printstacktrace(); } catch (invocationtargetexception e) { e.printstacktrace(); } } public void onpagescrollstatechanged(int state) {} public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {} /* method draws newest tab onto screen when * selected.*/ public void ontabselected(tab tab, fragmenttransaction ft) { mviewpager.setcurrentitem(tab.getposition()); sp = getsharedpreferences("prefs", mode_private); sharedpreferences.editor preferenceseditor = sp.edit(); preferenceseditor.putint("lastposition", mviewpager.getcurrentitem()); preferenceseditor.commit(); } public void ontabunselected(tab tab, fragmenttransaction ft) {} public void ontabreselected(tab tab, fragmenttransaction ft) {} public void ontabreselected(tab tab, android.app.fragmenttransaction ft) {} public void ontabunselected(tab tab, android.app.fragmenttransaction ft) {} }
well, want use 1 fragment class, basefragment, instead of multiple classes; doing passing string (identifier) new fragment. have code run in of these fragments preferable use new approach.
however, passing identifier basefragment cause code messy , have handle these if...else, when related identifier , code still same because have move code of specific fragments basefragment. hence, there no added value in approach. can keep fragments, , use "identifier" when adding tabs choose right fragment add.
as code run in many of fragments. basic approach:
create class basefragment extends fragment. (am being naive here) add function common() common thing. let fragments extend basefragment instead of fragment. now, can call common(). (again, might need common1(), common2(),...).
Comments
Post a Comment