TabLayout lower without AppToolbar

1

how do they walk? I do not know how to configure my tablayout to the bottom of my screen I am using fragments and my main view is a CoordinatorLayout. The idea of TabLayout is to use it without the Toolbar . This is my XML code

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".SendHelp">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

and in my OnCreate I have

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_help);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    mViewPager.setCurrentItem(2);
}

The idea of the view would be something like this:

    
asked by LcsGrz 10.07.2018 в 01:23
source

2 answers

-1

Based on your original layout, the way to do this is simple, instead of a CoordinatorLayout that is not really necessary use the ConstraintLayout . Currently it is important to know how ConstraintLayout works which will allow us to create complex layouts without loading the processor.

To position at the bottom the TabLayout use the property

app:layout_constraintBottom_toBottomOf="parent"

in this way you would position the TabLayout in the lower part of the parent container.

This would be the layout with the change:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".SendHelp">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toBottomOf="parent"
        />

</android.support.constraint.ConstraintLayout>

This would be the result:

    
answered by 10.07.2018 / 16:47
source
1

I would recommend using the BottomNavigationView element as follows, in fragment_vantispeople.xml :

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.FragmentVantisPeople"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <FrameLayout
                android:id="@+id/content_vantispeople"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            </LinearLayout>
        </ScrollView>
        <android.support.design.widget.BottomNavigationView
            android:background="@color/colorPrimary"
            app:itemTextColor="@color/colores_boton"
            app:itemIconTint="@color/colores_boton"
            android:layout_gravity="bottom"
            android:id="@+id/btn_vantispeople"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/vantispeople_menu"/>

    </LinearLayout>

in your FragmentVantisPeople.java :

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class FragmentVantisPeople extends Fragment {
    //index para los fragments
    private static final String TAG_FRAGMENT_A  = "a";    
    private static final String TAG_FRAGMENT_B  = "b";
    private static final String TAG_FRAGMENT_C  = "c";
    private static final String TAG_FRAGMENT_D  = "d";
    private static final String TAG_FRAGMENT_E  = "e";
    private static String CURRENT_TAG           = TAG_FRAGMENT_A;
    public  static int navItemIndex              = 0;
    private BottomNavigationView bnv;
    private Handler mHandler;
    private View view;

    public  FragmentVantisPeople() { }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_vantispeople, container, false);


        bnv             = view.findViewById(R.id.btn_vantispeople);
        mHandler        = new Handler();
        inicializarMenu();
        //Carga el primer fragment por defecto index 0
        if (savedInstanceState == null) {
            navItemIndex = 0;
            CURRENT_TAG = TAG_PERFIL;
            actualizarFragment();
        }

        return view;
    }

    private void inicializarMenu() { bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //Se enlazan los index con su repectivo menú
                switch (item.getItemId()) {
                    case R.id.btn_a:
                        navItemIndex    = 0;
                        CURRENT_TAG     = TAG_FRAGMENT_A;
                        break;
                    case R.id.btn_b:
                        navItemIndex    = 1;
                        CURRENT_TAG     = TAG_FRAGMENT_B;
                        break;
                    case R.id.btn_c:
                        navItemIndex    = 2;
                        CURRENT_TAG     = TAG_FRAGMENT_C;
                        break;
                    case R.id.btn_d:
                        navItemIndex    = 3;
                        CURRENT_TAG     = TAG_FRAGMENT_D;
                        break;
                    case R.id.btn_e:
                        navItemIndex    = 4;
                        CURRENT_TAG     = TAG_FRAGMENT_E;
                        break;
                    default:
                        navItemIndex    = 0;
                }
                actualizarFragment();
                return true;
            }
        });
    }

    private void actualizarFragment() {
        Runnable mPendingRunnable = new Runnable() {
            @Override
            public void run() {
                // Actualizar el fragment correspondiente en el "contenedor"
                Fragment fragment = obtenerFragmentActual();
                FragmentManager fragmentManager = getChildFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.fade_in,
                        R.anim.fade_out);
                fragmentTransaction.replace(R.id.content_vantispeople, fragment,CURRENT_TAG);
                fragmentTransaction.commitAllowingStateLoss();
            }
        };
        if (mPendingRunnable != null) {
            mHandler.post(mPendingRunnable);
        }
    }
    private Fragment obtenerFragmentActual() {
        switch (navItemIndex) {
            case 0:
                return new FragmentA();
            case 1:
                return new FragmentB();
            case 2:
                return new FragmentC();
            case 3:
                return new FragmentD();
            case 4:
                return new FragmentE();
            default:
                return new FragmentA();
        }
    }
}

Go to res > menu and create the file vantispeople_menu.xml :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/btn_a"
        android:title="Fragment A"
        android:icon="@drawable/img_a" />
    <item
        android:id="@+id/btn_b"
        android:title="Fragment B"
        android:icon="@drawable/img_b"/>
    <item
        android:id="@+id/btn_c"
        android:title="Fragment C"
        android:icon="@drawable/img_c"/>
    <item
        android:id="@+id/btn_d"
        android:title="Fragment D"
        android:icon="@drawable/img_d"/>
    <item
        android:id="@+id/btn_e"
        android:title="Fragment E"
        android:icon="@drawable/img_e"/>
</menu>
    
answered by 10.07.2018 в 15:58