json - Sliding Tabs - Android -
json - Sliding Tabs - Android -
i'm creating kind of horoscope. have 1 fragment horoscope signs, when user click on 1 horoscope sign, i'm sending sign name web service , i'm getting json horoscope content sign. content split in 4 parts: daily, weekly, monthly , yearly. i'm creating 4 tabs , filing content json. problem when go horoscope signs, 4 tabs still there. don't want tabs shown. need somehow hide 4 tabs, , show them 1 time again when user click on (or same) horoscope sign. when click on sign 4 new tabs created, old passed sign, not new. , whole screen white, there not content. here whole code:
fragmenthoroscopepage.java (the fragment horoscope signs):
public class fragmenthoroscopepage extends fragment implements onclicklistener { private fragmentitemhoroscopepage itemhoroscope; private fragmentmanager manager; private fragmenttransaction transaction; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_horoscope, container, false); log.d("fragment", "fragmenthoroscope"); manager = getfragmentmanager(); imageview ivoven = (imageview) view.findviewbyid(r.id.ivoven); imageview ivbik = (imageview) view.findviewbyid(r.id.ivbik); imageview ivbliznaci = (imageview) view.findviewbyid(r.id.ivbliznaci); imageview ivrak = (imageview) view.findviewbyid(r.id.ivrak); imageview ivlav = (imageview) view.findviewbyid(r.id.ivlav); imageview ivdevica = (imageview) view.findviewbyid(r.id.ivdevica); imageview ivvaga = (imageview) view.findviewbyid(r.id.ivvaga); imageview ivskorpija = (imageview) view.findviewbyid(r.id.ivskorpija); imageview ivstrelec = (imageview) view.findviewbyid(r.id.ivstrelec); imageview ivjarec = (imageview) view.findviewbyid(r.id.ivjarec); imageview ivvodolija = (imageview) view.findviewbyid(r.id.ivvodolija); imageview ivribi = (imageview) view.findviewbyid(r.id.ivribi); ivoven.setonclicklistener(this); ivbik.setonclicklistener(this); ivbliznaci.setonclicklistener(this); ivrak.setonclicklistener(this); ivlav.setonclicklistener(this); ivdevica.setonclicklistener(this); ivvaga.setonclicklistener(this); ivskorpija.setonclicklistener(this); ivstrelec.setonclicklistener(this); ivjarec.setonclicklistener(this); ivvodolija.setonclicklistener(this); ivribi.setonclicklistener(this); homecoming view; } @override public void onclick(view v) { itemhoroscope = new fragmentitemhoroscopepage(); transaction = manager.begintransaction(); bundle bundle = new bundle(); string sign = null; switch(v.getid()) { case r.id.ivoven: sign = "oven"; break; case r.id.ivbik: sign = "bik"; break; case r.id.ivbliznaci: sign = "bliznaci"; break; case r.id.ivrak: sign = "rak"; break; case r.id.ivlav: sign = "lav"; break; case r.id.ivdevica: sign = "devica"; break; case r.id.ivvaga: sign = "vaga"; break; case r.id.ivskorpija: sign = "skorpija"; break; case r.id.ivstrelec: sign = "strelec"; break; case r.id.ivjarec: sign = "jarec"; break; case r.id.ivvodolija: sign = "vodilija"; break; case r.id.ivribi: sign = "ribi"; break; } bundle.putstring("horoscope_sign", sign); itemhoroscope.setarguments(bundle); transaction.replace(r.id.maincontainer, itemhoroscope, "itemhoroscopepage"); transaction.addtobackstack("itemhoroscopepage"); transaction.settransition(fragmenttransaction.transit_fragment_open); transaction.commit(); } }
fragmentitemhoroscopepage.java - here i'm creating 4 tabs, viewpager , adapter
public class fragmentitemhoroscopepage extends fragment implements tablistener { private actionbar actionbar; private viewpager viewpager; private string sign; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.item_layout_horoscope, container, false); log.d("fragment", "fragmentitemhoroscope"); actionbar = getactivity().getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); fragmentmanager manager = getfragmentmanager(); sign = getarguments().getstring("horoscope_sign"); viewpager = (viewpager) view.findviewbyid(r.id.pager); viewpager.setadapter(new myadapter(manager)); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int i) { actionbar.setselectednavigationitem(i); } @override public void onpagescrolled(int i, float from, int pix) { } @override public void onpagescrollstatechanged(int i) { } }); actionbar.addtab(actionbar.newtab().settext(r.string.dneven).settablistener(this)); actionbar.addtab(actionbar.newtab().settext(r.string.nedelen).settablistener(this)); actionbar.addtab(actionbar.newtab().settext(r.string.mesecen).settablistener(this)); actionbar.addtab(actionbar.newtab().settext(r.string.godisen).settablistener(this)); homecoming view; } @override public void ontabselected(tab tab, fragmenttransaction ft) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } @override public void ontabunselected(tab tab, fragmenttransaction ft) { } class myadapter extends fragmentpageradapter { public myadapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int i) { bundle bundle = new bundle(); bundle.putstring("horoscope_sign", sign); fragment fragment = null; switch (i) { case 0: fragment = new fragmentdaily(); break; case 1: fragment = new fragmentweekly(); break; case 2: fragment = new fragmentmonthly(); break; case 3: fragment = new fragmentyearly(); break; } fragment.setarguments(bundle); homecoming fragment; } @override public int getcount() { homecoming 4; } } }
fragmentdaily.java - 1 of 4 tabs, others same, different content. here i'm getting passed horoscope_sign , in asynctask i'm sending post request web service, , json. when task done, i'm creating content of first tab (tab daily)
public class fragmentdaily extends fragment { private context context; private itemdaily itemdaily; private imageview ivznak; private textview tvznak; private textview tvday; private textview tvtext; private fragmentmanager manager; private fragmenttransaction transaction; private bundle bundle; private fragmentitemhoroscopepage itemhoroscopepage; private list<namevaluepair> pair; private string horoscope_sign; private string[] znak = new string[12]; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_dneven, container, false); log.d("horoscope fragment", "fragmentdaily"); context = getactivity(); manager = getfragmentmanager(); bundle = new bundle(); itemhoroscopepage = new fragmentitemhoroscopepage(); znak = context.getresources().getstringarray(r.array.horoscope); horoscope_sign = getarguments().getstring("horoscope_sign"); pair = new arraylist<namevaluepair>(); pair.add(new basicnamevaluepair("horoscope_sign", horoscope_sign)); system.out.println("unutra" + horoscope_sign); new gethoroscopetask().execute(); ivznak = (imageview) view.findviewbyid(r.id.ivdailyznak); tvznak = (textview) view.findviewbyid(r.id.tvdailyznak); tvday = (textview) view.findviewbyid(r.id.tvdailyday); tvtext = (textview) view.findviewbyid(r.id.tvdailytext); homecoming view; } class gethoroscopetask extends asynctask<string, void, boolean> { private progressdialog dialog; @override protected void onpreexecute() { super.onpreexecute(); dialog = new progressdialog(context); dialog.setmessage("please wait, colecting data!"); dialog.setcancelable(false); dialog.show(); } @override protected boolean doinbackground(string... params) { seek { defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(variables.url_horoscope); httppost.setentity(new urlencodedformentity(pair)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); string info = entityutils.tostring(httpentity); int status = httpresponse.getstatusline().getstatuscode(); if (status == httpstatus.sc_ok) { itemdaily = new itemdaily(); jsonobject jobject = new jsonobject(data); jsonobject jrealobject = jobject.getjsonobject("daily"); itemdaily.settext(jrealobject.getstring("text").tostring()); itemdaily.setday(jrealobject.getstring("day").tostring()); homecoming true; } else { toast.maketext(context, variables.error_message, toast.length_long).show(); ((activity) context).finish(); } } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (jsonexception e) { e.printstacktrace(); } homecoming false; } @override protected void onpostexecute(boolean result) { if(result == false) { toast.maketext(context, variables.error_message, toast.length_long).show(); } else { if(dialog.isshowing()) { dialog.dismiss(); } tvday.settext(getresources().getstring(r.string.dneven_horoskop) + " " + itemdaily.getday()); tvtext.settext(html.fromhtml(itemdaily.gettext())); } } } }
create new intent then. dont finish(); in tab activity. if ever wish go tabs 1 time again phone call finish();
android json tabs android-viewpager android-tabs
Comments
Post a Comment