//Clase ContenedorFragments
package com.example.bruno.proyecto.Fragmentos;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.support.design.widget.TabLayout;
import com.example.bruno.proyecto.Adaptador.SeccionAdaptador;
import com.example.bruno.proyecto.R;
import com.example.bruno.proyecto.clases.Utilidades;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ContenedorFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ContenedorFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ContenedorFragment extends Fragment {
private OnFragmentInteractionListener mListener;
//Declaracion de propiedades
private AppBarLayout AppBar;
private TabLayout Pestañas;
private ViewPager viewPager;
View vistas; //Puente entre referencias actividad y fragments
public ContenedorFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ContenedorFragment.
*/
// TODO: Rename and change types and number of parameters
public static ContenedorFragment newInstance(String param1, String param2) {
ContenedorFragment fragment = new ContenedorFragment();
Bundle args = new Bundle();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//Referencias
vistas = inflater.inflate(R.layout.fragment_contenedor, container, false);
if (Utilidades.rotacion==0)
{
View parent = (View) container.getParent(); //casting
//Validacion para que se instancie el appbar
if (AppBar == null)
{
AppBar = (AppBarLayout) parent.findViewById(R.id.id_AppBar); // Se crea el AppBar
Pestañas = new TabLayout (getActivity()); // Instancia para la pestaña
//Pestañas.setTabTextColors(Color.parseColor("#FFFF"), Color.parseColor("#FFFF"));// Color para las pestañas
AppBar.addView(Pestañas); //Se agregan las pestañas a AppBar
viewPager = (ViewPager) vistas.findViewById(R.id.idViewPagerInformacion); //Vincula la logica de navegacion con los fragments y gestos de arraste de pantalla
LLenarViewPager(viewPager);//Llamado al metodo que se encargar de llenar el contenedor con los fragmentos
// Evento para arrastar la pantalla
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
// Metodo que se encarga de agregar scroll o llamado a pantalla
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
});
Pestañas.setupWithViewPager(viewPager);
}
Pestañas.setTabGravity(TabLayout.GRAVITY_FILL);
}else
Utilidades.rotacion=1;
return vistas;
}
private void LLenarViewPager(ViewPager viewPager) {
SeccionAdaptador Adaptador = new SeccionAdaptador(getFragmentManager());
Adaptador.addFragment(new SensorFragment(), "Puertas "); // LLama a los fragmentos para las pestañas
Adaptador.addFragment(new MotorFragment(), "Motor "); // LLama a los fragmentos para las pestañas
Adaptador.addFragment(new EventosFragment(), "Eventos "); // LLama a los fragmentos para las pestañas
viewPager.setAdapter(Adaptador);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (Utilidades.rotacion==0)
{
AppBar.removeView(Pestañas);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}