Good afternoon. I want to make an app where I have a side menu, with standard options, such as "home page, profile, etc ...". When I click home page, I want my screen to turn a tablayout with two tabs. For this, I made on my main screen the side menu, and a relative layout, where I will put my fragments to occupy the screen. but when I put my fragment containing the tabs, it occupies the entire screen, not appearing the pages that I add in it. At first, for testing, I just did the tabs, straight into the main layout, no fragments, and everything was ok. I wanted to know how to make it right ... I already tried to change almost everything that gave of the widths and heigths, but nothing worked.
.java of the flaps fragment
public FragAbas() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_frag_abas, container, false);
ViewPager viewPager = rootView.findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new FragOferta(), "Ofertas");
adapter.addFragment(new FragProcura(), "Procuras");
viewPager.setAdapter(adapter);
TabLayout tabLayout = rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
}
Tabs Frag xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_below="@id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabTextColor="#000000"
app:tabSelectedTextColor="@color/colorAccent" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_below="@id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
.java menu
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicial);
setMenu();
setFragOnMenu();
}
private void setMenu() {
mDrawerLayout = findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void setFragOnMenu(){
NavigationView nv = findViewById(R.id.menuLateral);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fr = null;
switch (item.getItemId()) {
case R.id.nav_pag_inicial:
Toast.makeText(getApplicationContext(), "aaah", Toast.LENGTH_SHORT).show();
fr = new FragAbas();
break;
case R.id.nav_como_funciona:
fr = null;
break;
case R.id.nav_perfil:
fr = null;
break;
case R.id.nav_dados_pessoais:
fr = null;
break;
case R.id.nav_negociacoes:
fr = null;
break;
case R.id.nav_sobre:
fr = null;
break;
case R.id.nav_sair:
fr = null;
break;
default:
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (fr != null){
fragmentTransaction.replace(R.id.relativeContainer, fr).commit();
}
item.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
Menu xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeContainer">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navegation_menu"
android:layout_gravity="start"
android:id="@+id/menuLateral">
</android.support.design.widget.NavigationView>