Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Navigation Bar not working inside fragment

I am trying to show a Bottom Navigation Bar with 3 items inside a fragment, and it shows correctly without errors in my code. But the thing is that when I click on one of the options, the code supposed to be run doesn't work, it doesn't even log anything on the console.

TransporteFragment.Java:

import android.os.Bundle;
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.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class TransporteFragment extends Fragment {


    public TransporteFragment() {
        // Required empty public constructor
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_busC1:

                    return true;
                case R.id.action_busC2:
                    BusC2Fragment busC2Fragment = new BusC2Fragment();
                    FragmentManager fragmentManager = getChildFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.espacioLineas, busC2Fragment).commit();
                    return true;
                case R.id.action_busC3:

                    return true;
            }
            return false;
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_transporte, container, false);

        BottomNavigationView navegacion = (BottomNavigationView) v.findViewById(R.id.navbartransporte);
        navegacion.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_transporte, container, false);
    }

}

fragment_transporte.xml:

<RelativeLayout 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="com.gberti.smarthuesca.TransporteFragment">



    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/espacioLineas">

    </FrameLayout>
    <android.support.design.widget.BottomNavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navbartransporte"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        app:menu="@menu/navbar_transporte_items" />
</RelativeLayout>

Thank you.

like image 798
Alejandro Bertinelli Avatar asked Aug 13 '17 18:08

Alejandro Bertinelli


People also ask

How to set Fragment in Bottom Navigation Android?

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. For this example, create a basic app with a FrameLayout and a Bottom Navigation Bar. The FrameLayout will contain Fragments which will change as the user click on the items in the Bottom Navigation Bar.

How to create Bottom Menu in Android studio?

In an existing Android Studio project, to use this template, simply go to File > New > Activity > Bottom Navigation Activity.


Video Answer


1 Answers

In your onCreateView method instead of return inflater.inflate(R.layout.fragment_transporte, container, false); try return v;

like image 161
MidasLefko Avatar answered Oct 21 '22 00:10

MidasLefko