Custom Android TabHost Slow Fragments -
i use actionbarsherlock , slidingmenu.
my mainactivity not access db , no parsing of ever. static.
tabs generated dynamicly depending on "section" choose slidingmenu. after 2 click app becomes awefully slow.
below main view.
<tabhost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <tabwidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" /> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#0000ff" > </framelayout> <framelayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" > </framelayout> </linearlayout> </tabhost> </linearlayout>
the code used manage tabhost & fragments :
public void selectpage(menupageitem page, page subnavigationpage) { if (page == null || subnavigationpage == null || (page.equals(selectedpage) && subnavigationpage.equals(selectedsubnavigation))) { return; } log.d(tag, "show page: " + page.gettitle() + " / subnavigationpage: " + subnavigationpage.gettitle()); boolean needupdatetabui = !(page.equals(selectedpage)); selectedpage = page; selectedsubnavigation = subnavigationpage; // 1) manage tabhost if (needupdatetabui) { log.d(tag, "need update tab layout"); updatepagetabui(); } // change selected tab mtabhost.setcurrenttabbytag(selectedsubnavigation.gettitle()); // 2) change contentfragment log.d(tag, "update fragment"); // todo: check current fragment not 1 , // call updatedata method updatepagefragment(); // close sliding menu if opened if (getslidingmenu().isshown()) { handler h = new handler(); h.postdelayed(new runnable() { public void run() { getslidingmenu().showcontent(); } }, 50); } } private void updatepagetabui() { if (selectedpage.getpages().size() > 1) { // more 1 sub navigation -> show tab host // mtabhost.setvisibility(view.visible); mtabhost.setup(); mtabhost.clearalltabs(); int = 0; (final page subnav : selectedpage.getpages()) { log.d(tag, "add tab tag=" + subnav.gettitle() + " title=" + subnav.gettitle()); mtabhost.addtab(mtabhost.newtabspec(subnav.gettitle()).setindicator(subnav.gettitle()) .setcontent(new emptytabfactory())); mtabhost.gettabwidget().getchildat(i).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { selectpage(selectedpage, subnav); } }); i++; } } else { // 1 sub navigation -> hide tab host // mtabhost.setvisibility(view.gone); mtabhost.setup(); mtabhost.clearalltabs(); } mtabhost.requestlayout(); mtabhost.invalidate(); } private void updatepagefragment() { try { pagefragment fragcontent = (pagefragment) selectedsubnavigation.getfragmentclass().newinstance(); fragcontent.setsubnavigationpage(selectedsubnavigation); fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.content_frame, fragcontent); fragmenttransaction.commit(); } catch (instantiationexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } }
you keep entire work on uithread , therefore stumble upon slow response (for example 1 of reasons why forbidden on latest android os networking on ui thread). try changing concept little bit , tab creation in background on different thread. or @ least lock ui , show wait dialog when work in order user have proper ui experience.
hope helps , enjoy work.
Comments
Post a Comment