android - Using PreferenceFragment in SherlockFragmentActivity with tabs -


i trying make tabbed activity using abs sherlockfragmentactivity viewpager, , have 1 of tabs contain preference activity/fragment.

as abs not have sherlockpreferencefragment, or kind of support preferencefragment, found possible solution using unifiedpreference

unifiedpreference works on it's own, demo works great, if try implement in actionbarsherlock powered tabs, crashes message:

04-18 03:59:26.000: e/androidruntime(8437): fatal exception: main 04-18 03:59:26.000: e/androidruntime(8437): java.lang.classcastexception: si.neanonimen.settings$netsettingsprefsfragment cannot cast android.support.v4.app.fragment 04-18 03:59:26.000: e/androidruntime(8437):     @ android.support.v4.app.fragment.instantiate(fragment.java:384) 

it looks unifiedpreferencefragment not extend android.support.v4.app.fragment. there way around this? has managed working example?

my current code is:

settings:

public class settings extends unifiedsherlockpreferenceactivity {      public static string tag = "settings";      @override     public void oncreate(bundle savedinstancestate) {         setheaderres(r.xml.pref_headers);         super.oncreate(savedinstancestate);      }      public static class netsettingsprefsfragment extends unifiedpreferencefragment {} } 

tabbed activity:

public class fragmenttabs extends sherlockfragmentactivity {     private tabhost     mtabhost;     private viewpager   mviewpager;     private tabsadapter mtabsadapter;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.fragment_tabs);         mtabhost = (tabhost) findviewbyid(android.r.id.tabhost);         mtabhost.setup();          mviewpager = (viewpager) findviewbyid(r.id.pager);          mtabsadapter = new tabsadapter(this, mtabhost, mviewpager);          // problem         mtabsadapter.addtab(mtabhost.newtabspec("settings").setindicator("nastavitve"), settings.netsettingsprefsfragment.class, null);          if (savedinstancestate != null) {             mtabhost.setcurrenttabbytag(savedinstancestate.getstring("tab"));         }     }      @override     protected void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);         outstate.putstring("tab", mtabhost.getcurrenttabtag());     }      public static class tabsadapter extends fragmentpageradapter implements tabhost.ontabchangelistener, viewpager.onpagechangelistener {         private final context               mcontext;         private final tabhost               mtabhost;         private final viewpager             mviewpager;         private final arraylist<tabinfo>    mtabs   = new arraylist<tabinfo>();          static final class tabinfo {             //private final string  tag;             private final class<?>  clss;             private final bundle    args;              tabinfo(string _tag, class<?> _class, bundle _args) {                 //tag = _tag;                 clss = _class;                 args = _args;             }         }          static class dummytabfactory implements tabhost.tabcontentfactory {             private final context   mcontext;              public dummytabfactory(context context) {                 mcontext = context;             }              @override             public view createtabcontent(string tag) {                 view v = new view(mcontext);                 v.setminimumwidth(0);                 v.setminimumheight(0);                 return v;             }         }          public tabsadapter(fragmentactivity activity, tabhost tabhost, viewpager pager) {             super(activity.getsupportfragmentmanager());             mcontext = activity;             mtabhost = tabhost;             mviewpager = pager;             mtabhost.setontabchangedlistener(this);             mviewpager.setadapter(this);             mviewpager.setonpagechangelistener(this);         }          public void addtab(tabhost.tabspec tabspec, class<?> clss, bundle args) {             tabspec.setcontent(new dummytabfactory(mcontext));             string tag = tabspec.gettag();              tabinfo info = new tabinfo(tag, clss, args);             mtabs.add(info);             mtabhost.addtab(tabspec);             notifydatasetchanged();         }          @override         public int getcount() {             return mtabs.size();         }          @override         public fragment getitem(int position) {             tabinfo info = mtabs.get(position);             return fragment.instantiate(mcontext, info.clss.getname(), info.args);         }          @override         public void ontabchanged(string tabid) {             int position = mtabhost.getcurrenttab();             mviewpager.setcurrentitem(position);         }          @override         public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {}          @override         public void onpageselected(int position) {             // unfortunately when tabhost changes current tab, kindly             // takes care of putting focus on when not in touch mode.             // jerk.             // hack tries prevent pulling focus out of our             // viewpager.             tabwidget widget = mtabhost.gettabwidget();             int oldfocusability = widget.getdescendantfocusability();             widget.setdescendantfocusability(viewgroup.focus_block_descendants);             mtabhost.setcurrenttab(position);             widget.setdescendantfocusability(oldfocusability);         }          @override         public void onpagescrollstatechanged(int state) {}     } } 

i need implement preferencefragment in android versions prior honeycomb (api level 11). have, found unifiedpreference library looking @ source code see unifiedpreferencefragment extends preferencefragment official android api, not working < api level 11.

this class i've found out there, haven't tested yet:

hope helps!

have found solution? android folk add class support library soon!

cheers,


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -