android - ActionBar: Swipe between tabs -
i make possible swipe between tabs in app. have found plenty tutorials this, can't work code, because use different code. myactivity.class:
public class myactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tab taba = actionbar.newtab(); taba.seticon(r.drawable.icon_settings); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayshowtitleenabled(false); taba.settext(""); taba.settablistener(new com.example.mainactivity.tablistener<tab1>(this, "tab1", tab1.class)); actionbar.addtab(taba); tab tabb = actionbar.newtab(); tabb.settext("tab2"); tabb.settablistener(new com.example.mainactivity.tablistener<tab2>(this, "tab2", tab2.class)); actionbar.addtab(tabb); tab tabc = actionbar.newtab(); tabc.settext("tab3"); tabc.settablistener(new com.example.mainactivity.tablistener<tab3>(this, "tab3", tab3.class)); actionbar.addtab(tabc); if (savedinstancestate != null) { int savedindex = savedinstancestate.getint("saved_index"); getactionbar().setselectednavigationitem(savedindex); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <tabhost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingtop="5dip"> <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"> </tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:id="@+id/einstellungen" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ffffff"> </linearlayout> <linearlayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </linearlayout> <linearlayout android:id="@+id/tab2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </linearlayout> <linearlayout android:id="@+id/tab3" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </linearlayout> </framelayout> </linearlayout> </tabhost>
tab1.class
public class tab1 extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view myfragmentview = inflater.inflate(r.layout.fragment_a, container, false); return myfragmentview; } }
tablistener.class
public class tablistener <t extends fragment> implements actionbar.tablistener{ private final activity myactivity; private final string mytag; private final class myclass; public tablistener(activity activity, string tag, class<t> cls) { myactivity = activity; mytag = tag; myclass = cls; } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { fragment myfragment = myactivity.getfragmentmanager().findfragmentbytag(mytag); if (myfragment == null) { myfragment = fragment.instantiate(myactivity, myclass.getname()); ft.add(android.r.id.content, myfragment, mytag); } else { ft.attach(myfragment); } } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { fragment myfragment = myactivity.getfragmentmanager().findfragmentbytag(mytag); if (myfragment != null) { ft.detach(myfragment); } } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }
you can easier using default actionbar tabs + swipe views
of eclipse
, android sudio
or intellij idea
. anyway can take @ answer here android actionbar tabs - swipe or here creating swipe views tabs can find nice tutorial android developers
.
hope helped you
Comments
Post a Comment