¿Cómo abrir la pestaña particular dentro de Tab Layout en el clic de los elementos del menú de la navegación de cajón?

En realidad i nuevo en Android y se quedó atrapado en el cajón de navegación .. he diseñado con éxito diseñado el cajón de navegación y sus elementos de menú. Pero cuando hice clic en ese menú que abrir un fragmento independiente, mientras que quiero que se abra en determinadas pestañas dentro de Tab Layout.

He hecho separar la disposición de la lengüeta con el adaptador de encargo. Mytablayout archivo .xml es como

<?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> 

Mi clase de Tabfragment está allí:

 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; } } 

Mi SplitviewActivity se muestra así:

  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); } } 

Mi menú de navegación item.xml son los siguientes:

 <?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> 

Todo está bien excepto. Todas las tres pestañas han sido Shown.navigation cajón también funcionan bien.

La única cosa que no podía entender que cómo puedo abrir un fragmento en la lengüeta en el tecleo de la navegación menuitems.At actual cuando hago clic en el artículo del menú, abre un fragmento independiente no en la lengüeta. ¿Cómo puedo conseguir esto. Cualquier ayuda Sería Apreciado en avanzado.

Basta con comprobar en el cajón onClick la posición de clic y comprobar contra cada pestañas viewpager que ficha es lo que quieres y utilizar Viewpager.setCurrentItem(posiition)

Para navegar hasta esa pestaña.

 public void setStartTab(String topTab) { LocalLog.e("TOPTAB",topTab+""); if(topTab==null){ mViewPager.setCurrentItem(0); }else{ for(int i=0;i<mTabs.size();i++){ LocalLog.e("TOPTAB",mTabs.get(i).name); if(mTabs.get(i).name.equalsIgnoreCase(topTab)){ LocalLog.e("TOPTAB",i+"true"); mViewPager.setCurrentItem(i); } } } } 
 activity_main.xml 

 <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

 public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new OneFragment(), "ONE"); adapter.addFragment(new TwoFragment(), "TWO"); adapter.addFragment(new ThreeFragment(), "THREE"); viewPager.setAdapter(adapter); } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } 

}

Sus trabajos para mí

  • Efecto de desplazamiento del menú del cajón de navegación
  • Elementos del menú de vista de navegación, como Google Play Store
  • NavigationView y diseño personalizado
  • Cómo agregar un elemento de menú plegable dentro del cajón de navegación en android?
  • NavigationView: ¿cómo insertar divisor sin subgrupo?
  • ¿Cuál es el tamaño de los iconos del cajón de navegación?
  • Implementación de la retroinstalación adecuada y manejo de botones de inicio mediante la barra de herramientas en Android
  • Desplazamiento de la animación en NavigationView
  • Problema para quitar el elemento seleccionado del menú de navegación?
  • Deshabilitar un tinte de color en NavigationView sólo para los iconos especificados
  • En android cómo configurar la imagen de encabezado de cajón de navegación y el nombre de forma programática en el archivo de clase?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.