android - using a FragmentTabHost with a ViewPager -
i want create swipe tab interface using fragmenttabhost
viewpager
. far got fragments slide. thing that's happening fragments able slide behind tabs disabled/stuck , not working. how can make fragmenttabhost tabs work viewpager swiping between tabs?
i tried using fragmenttabhost without viewpager, works fine, not working viewpager.
here's screenshot of looks like:
main java source code:
import java.util.list; import java.util.vector; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.app.fragmenttabhost; import android.support.v4.view.viewpager; public class mainactivity extends fragmentactivity { private fragmenttabhost mtabhost; private viewpager pager; private mypageradapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtabhost = (fragmenttabhost)findviewbyid(android.r.id.tabhost); pager = (viewpager) findviewbyid(r.id.viewpager); list<fragment> fragments = new vector<fragment>(); fragments.add(fragment.instantiate(this, batteryfragment.class.getname())); fragments.add(fragment.instantiate(this, devicefragment.class.getname())); fragments.add(fragment.instantiate(this, networkfragment.class.getname())); adapter = new mypageradapter(getsupportfragmentmanager(), fragments); pager.setadapter(adapter); mtabhost.setup(this, getsupportfragmentmanager(), r.id.realtabcontent); mtabhost.addtab(mtabhost.newtabspec("battery").setindicator("battery", getresources().getdrawable(r.drawable.ic_battery_tab)), batteryfragment.class, null); mtabhost.addtab(mtabhost.newtabspec("network").setindicator("network", getresources().getdrawable(r.drawable.ic_network_tab)), networkfragment.class, null); mtabhost.addtab(mtabhost.newtabspec("device").setindicator("device", getresources().getdrawable(r.drawable.ic_device_tab)), devicefragment.class, null); } public class mypageradapter extends fragmentpageradapter { private list<fragment> fragments; public mypageradapter(fragmentmanager fm, list<fragment> fragments) { super(fm); this.fragments = fragments; } @override public fragment getitem(int arg0) { // todo auto-generated method stub return this.fragments.get(arg0); } @override public int getcount() { // todo auto-generated method stub return this.fragments.size(); } } }
xml code:
<linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <tabwidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <framelayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <framelayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" /> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> </linearlayout> </android.support.v4.app.fragmenttabhost>
add following code:
pager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { // todo auto-generated method stub mtabhost.setcurrenttab(arg0); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } });
the problem when swiping fragments there no way tabs know it,by specifying through above code,you can make work. had same problem solved through this.hope helps.
Comments
Post a Comment