0

estoy colocando un Menu Bottom en un fragment, antes lo tenía configurado en un CordinatorLayout y generaba todo el código desde el MainActivity, pero ahora lo quiero colocar en un fragment y que desde ahi escuche los eventos, lo que necesito es saber como declararlo en el Fragment. A continuación muestro el código xml y el Fragment, tambien les muestro el código que usaba antes en el MainActivity para que me ayuden a pasarlo al Fragmento, muchas gracias.

XML

<FrameLayout 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"
android:background="#eddd76"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.tecnologias.uniagustapp.fragmentos.Fragment_Home">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="58dp"
    android:background="#eddd76">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/main03"
        tools:ignore="ContentDescription" />

</LinearLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:background="#202240"
    app:menu="@menu/navigation" />

Fragment

public class Fragment_Home extends Fragment {

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment__home, container, false);

    return v;
}
}

A continuación el código que antes usaba en el MainActivity

private BottomNavigationView bottomNavigationView;

//BOTTON NAVIGATION VIEW

    final Fragment home = new Home();
    final Fragment noticias = new Noticias();
    final Fragment calendario = new Calendario();
    final Fragment ubicacion = new Map_Fragment();
    final Fragment pqrs = new PQRS();
    final Fragment preinscrip = new PreInscripcion();

bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
    bottomNavigationView.setItemIconTintList(null);//Los iconos del menu Bottom toman su color original
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            FragmentManager fragmentManager = getFragmentManager();

            if (item.getItemId() == R.id.noticias) {
                //FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //fragmentTransaction.replace(R.id.content_main, noticias).commit();

            } else if (item.getItemId() == R.id.rutas) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_main, ubicacion).commit();
            } else if (item.getItemId() == R.id.cal_aca) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_main, calendario).commit();
            } else if (item.getItemId() == R.id.pqrs) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_main, pqrs).commit();
            } else if (item.getItemId() == R.id.preinscripcion) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_main, preinscrip).commit();
            }
            return true;
        }
    });//*
1
  • Ya probaste implementarlo? y si lo hiciste que error te dio? el 31 oct. 2017 a las 16:03

1 respuesta 1

1

Puedes hacerlo en el onViewCreated, si declaras en el onCreateView el view como global. Ejemplo:

private View root;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment__home, container, false);

        return root;
    }

 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    ...
     bottomNavigationView = (BottomNavigationViewEx) root.findViewById(R.id.navigation);



...
     // El resto de tu implementación.

    }

Si ya lo has intentado y tienes errores, ¿cuales son?

1
  • Muchas gracias, acomodé todo el código en el onViewCreated y ajusté el contexto. ahora quisiera intentar que el menú se oculte como lo hace el menú lateral Drawer, sin embargo aun no tengo la idea clara mientras organizo la idea. el 2 nov. 2017 a las 21:28

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.