aldakur
aldakur

Reputation: 419

How to change a TextView Fragment by a button located on the ActionBar (In the main fragment)

I have a main FragmentActivity (InfoInstalacionDetailsCollection extends FragmentActivity implements ViewPager.OnPageChangeListener) with ViewPager.

When the viewPager is in the Fragment number 4 (public class InfoInstalacionFragment4 extends Fragment), there appears a button (R.id.info_instalacion_details_collection_actionbar_menu_btn_buscar) on the ActionBar. By clicking the button, I want the TextView values of the current Fragment (number 4) to change.

How can I do that?

This is my code:

In InfoInstalacionDetailsCollection. onCreat method i declare ActionBar:

    actionBar = new ActionBarImplementation(this, this, R.string.app_name, OPCION_MENU);

and then:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.info_instalacion_details_collection_actionbar_menu, menu);
    MenuItem btnVeranoInvierno = menu.findItem(R.id.info_instalacion_details_collection_actionbar_menu_btn_buscar);
    btnVeranoInvierno.setVisible(false);

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
    if(item.getItemId()== R.id.info_instalacion_details_collection_actionbar_menu_btn_buscar){
            Log.d("Page", "InfoInstalacionFragment4. Verano: ");
    }

    return true;
}

//Menu de ActionBar
public boolean onPrepareOptionsMenu(Menu menu) {
    int pageNum =  mViewPager.getCurrentItem();

    Log.e("Page", "InfoInstalacionDetailsCollection. onPrepareOptionsMenu. pagina actual: "+pageNum);
    if(pageNum== 3){      
    MenuItem btnVeranoInvierno = menu.findItem(R.id.info_instalacion_details_collection_actionbar_menu_btn_buscar).setVisible(true);
    btnVeranoInvierno.setVisible(true);
    Log.e("Page", "InfoInstalacionDetailsCollection. onPrepareOptionsMenu. pagina actual: "+pageNum);
    }else{            
    menu.findItem(R.id.info_instalacion_details_collection_actionbar_menu_btn_buscar).setVisible(false);
    Log.e("Page", "InfoInstalacionDetailsCollection. onPrepareOptionsMenu. pagina actual: "+pageNum);
    }
    return super.onPrepareOptionsMenu(menu);
}



@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int pageNum) {
    // TODO Auto-generated method stub
    invalidateOptionsMenu();//Llama al metodo del sistema onPrepareOptionsMenu()
  }

And this is my code InfoInstalacionFragment4 (onCreateView method)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //Creamos la View
    View view = inflater.inflate(R.layout.infoinstalacion_fragment4, container, false);

    tv_titulo = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_titulo);
    tv_subtitulo = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_subtitulo);
    tv_horariosEspeciales = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_horarios_especiales);
    tv_diasDeCierre = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_dias_de_cierre);
    tv_lunesViernesHora = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_lunes_viernes_hora);
    tv_sabadosHora = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_sabados_hora);
    tv_domingosHora = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_domingos_hora);
    tv_festivosHora = (TextView) view.findViewById(R.id.infoinstalacion_fragment4_tv_festivos_hora);        
    tv_horariosEspeciales.setOnClickListener(this);
    tv_diasDeCierre.setOnClickListener(this);

    if(summer){
        tv_titulo.setText(R.string.horario_de_verano);
        tv_subtitulo.setText(R.string.del_14_de_junio_al_31_de_agosto);
        tv_lunesViernesHora.setText(verHorarioLunVie);
        tv_sabadosHora.setText(verHorarioSab);
        tv_domingosHora.setText(verHorarioDom);
        tv_festivosHora.setText(verHorarioFest);

    }else{
        tv_titulo.setText(R.string.horario_de_invierno);
        tv_subtitulo.setText(R.string.del_1_de_septiembre_al_13_de_junio);
        tv_lunesViernesHora.setText(invHorarioLunVie);
        tv_sabadosHora.setText(invHorarioSab);
        tv_domingosHora.setText(invHorarioDom);
        tv_festivosHora.setText(invHorarioFest);
    }

    return view;
}

thank you very much!

Upvotes: 0

Views: 126

Answers (1)

Leonardo C
Leonardo C

Reputation: 276

You need to keep a reference of the InfoInstalacionFragment4 instance you are using in the pager within the activity , so you can access it when the user pressed on the menu item.

The way you keep the reference depends on where the ViewPager is located. If it is within the activity , make a field in the activity and assign the new InfoInstalacionFragment4 to it.

In the InfoInstalacionFragment4 you can add a method that , say public void changeTextViews().

When the user presses the menu , you call that method on the instance you have of InfoInstalacionFragment4.

Upvotes: 1

Related Questions