How to open particular Tab inside Tab Layout on click of menu items of Navigation Drawer?

Viewed 2386

Actually i new in Android and got stuck in Navigation Drawer.. i have successfully designed material designed navigation drawer and their menu items . But when i clicked on that menu it open a independent fragment while i want it to open it in particular tabs inside Tab Layout.

i have made separate Tab layout with custom Adapter. mytablayout .xml file is as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:elevation="0dp"
    android:background="#6ec6c5"
    app:tabIndicatorColor="#000000"
    app:tabSelectedTextColor="@color/textColor"
    app:tabTextColor="#A8DCDC"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>
</LinearLayout>

my Tabfragment class are there:

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
    /**
     *Inflate tab_layout and setup Views.
     */
    View x = inflater.inflate(R.layout.fragment_tab, null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager(),int_items ));

    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

    return x;
  }
}

MyAdapter is :

 public class MyAdapter extends FragmentPagerAdapter {
  int int_items;
  public MyAdapter(FragmentManager fm,int int_items) {
    super(fm);
    this.int_items = int_items;
}

/**
 * Return fragment with respect to Position .
 */

@Override
public Fragment getItem(int position)
{
    switch (position){
        case 0 : return new ProductFragment();
        case 1 : return new ProductFragment();
        case 2 : return new ProductFragment();
    }
    return null;
}

@Override
public int getCount() {

    return int_items;

}

/**
 * This method returns the title of the tab according to the position.
 */

@Override
public CharSequence getPageTitle(int position) {

    switch (position){
        case 0 :
            return "PRODUCTS";
        case 1 :
            return "FEATURED";
        case 2 :
            return "FAVOURITES";
    }
    return null;
 }
}

My SplitviewActivity is shown like this:

 public class SplitViewActivity extends AppCompatActivity {

  private DrawerLayout mDrawerLayout;
  private NavigationView navigationView;
  FragmentManager mFragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_split_view);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();

            switch (menuItem.getItemId()) {

                case R.id.navigation_item_products:

                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();


                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    // updateDisplay(new AttachmentFragment());
                    break;

                case R.id.navigation_item_new_Releases:
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new ProductFragment()).commit();

                 /*   FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.frame_container,new ProductFragment()).commit();*/
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    // updateDisplay(new ImageFragment());
                    break;

                case R.id.navigation_item_favorites:

                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    // updateDisplay(new MyLocationFragment());
                    break;

                case R.id.navigation_item_about_us:
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    // updateDisplay(new MyLocationFragment());
                    break;
                case R.id.navigation_item_notification:
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    //  updateDisplay(new MyLocationFragment());
                    break;

                case R.id.navigation_sub_item_01:
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.navigation_sub_item_02:
                    Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 02 Clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
        case R.id.action_settings:
            return true;
    }

    return super.onOptionsItemSelected(item);
 }
}

my navigation menu item.xml file are as :

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_products"
        android:checked="true"
        android:title="Products" />
    <item
        android:id="@+id/navigation_item_new_Releases"
        android:title="New Releases" />
    <item
        android:id="@+id/navigation_item_favorites"
        android:title="favorites" />
    <item
        android:id="@+id/navigation_item_about_us"
        android:title="About Us" />
    <item
        android:id="@+id/navigation_item_notification"
        android:title="Notifications" />

</group>

<item android:title="">
    <menu>
        <item
            android:id="@+id/navigation_sub_item_01"
            android:title="Configure" />
        <item
            android:id="@+id/navigation_sub_item_02"
            android:title="Logout" />
    </menu>
</item>

Everything is work fine except.All three tabs has been Shown.navigation drawer also work well.

Only thing that i couldn't understand that how can i open a fragment in tab on click of navigation menuitems.At present when i click on menu item,it open an independent fragment not in tab.How can i achieve this ..Any help would be Appreciated in advanced..

2 Answers
Related