Tabllayout in a Fragment occupying all space

Asked

Viewed 52 times

1

Good afternoon. I want to make an app where I have a side menu, with default options, like "home page, profile, about etc...". When I click on the home page, I want my screen to turn into a tablayout, with two tabs. For this, I made in my main screen the side menu, and a relative layout, where I will put my Ragments to occupy the screen. However, when I put my Ragment containing the Tabs, it occupies the entire screen, not showing the pages I add to it. At the beginning, for testing, I only made the tabs, right in the main layout, without Ragments, and everything was ok. I wanted to know how to make it right... I tried to change almost everything I gave of widths and Heigths, but nothing worked.

.java from the tab 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;
}

}

Frag xml of tabs

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

.menu java

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

xml menu

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

Como está ficandoComo era

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.