java - how to show tab of view pager on click of navigation drawer item without overlaping? -
there 3 fragments in app.i showed them in tabs successfully.now want show fragment of tab1 on click of navigationdrawer item1 , same rest.but there problem of overlapping.can guide me rid of problem?
mainactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); viewpager viewpager = (viewpager) findviewbyid(r.id.pager); // set viewpageradapter viewpager viewpager.setadapter(new viewpageradapter(getsupportfragmentmanager())); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); // handle navigation view item clicks here. int id = item.getitemid(); if (id == r.id.nav_camera) { fragmentone f1 = new fragmentone(); fragmenttransaction.replace(r.id.fragment_container,f1); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); getsupportactionbar().settitle("camera"); } else if (id == r.id.nav_gallery) { fragmenttwo f2 = new fragmenttwo(); fragmenttransaction.replace(r.id.fragment_container,f2); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); getsupportactionbar().settitle("gallery"); } else if (id == r.id.nav_slideshow) { menufragment f3 = new menufragment(); fragmenttransaction.replace(r.id.fragment_container,f3); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); getsupportactionbar().settitle("list"); } else if (id == r.id.nav_manage) { } else if (id == r.id.nav_share) { } else if (id == r.id.nav_send) { } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; } }
fragmentone.java
public class fragmentone extends fragment { textview t1; public fragmentone(){ } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub return inflater.inflate(r.layout.fragment_one_layout,container, false); } }
viewpageradapter.java
public class viewpageradapter extends fragmentpageradapter { final int page_count = 3; // tab titles private string tabtitles[] = new string[] { "tab1", "tab2", "tab3" }; context context; public viewpageradapter(fragmentmanager fm) { super(fm); } @override public int getcount() { return page_count; } @override public fragment getitem(int position) { switch (position) { // open fragmenttab1.java case 0: fragmentone fragmenttab1 = new fragmentone(); return fragmenttab1; // open fragmenttab2.java case 1: fragmenttwo fragmenttab2 = new fragmenttwo(); return fragmenttab2; // open fragmenttab3.java case 2: menufragment fragmenttab3 = new menufragment(); return fragmenttab3; } return null; } @override public charsequence getpagetitle(int position) { return tabtitles[position]; } }
mainactivity.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.chaitanya.navbar.mainactivity" tools:showin="@layout/app_bar_main"> <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.pagertabstrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingbottom="10dp" android:paddingtop="10dp" android:textcolor="#000000" /> </android.support.v4.view.viewpager> <relativelayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|center_horizontal" ></relativelayout>
is there reason using fragmenttransaction? call
viewpager.setcurrentitem(0);
the argument pass (0 or 1 or 2 or ...) based on action click in navigationview. tabs should update automatically when viewpager changes.
Comments
Post a Comment