更改标签时执行某些操作

时间:2017-07-12 22:38:28

标签: java android

更改标签时如何执行某些操作?像方法或东西有什么简单的方法?我已经搜索过了,但是我发现了一些名为TabHost的东西,任何人都可以解释一下吗?

这是我的标签代码:

     ublic class Testes extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
private Caldroid_fragment cf;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testes);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    cf = new Caldroid_fragment();



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



}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_testes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()){
       case go_to_fazer_media:{
           if (item.isChecked())
               item.setChecked(false);
            else{
           Intent i = new Intent(Testes.this,Formas.class);
           startActivity(i);
               return true;
            }
       }
       case definicoes:{
           if (item.isChecked())
               item.setChecked(false);
           else{
              return true;
           }
       }
   }


    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){

            case 0:
                Tab1tests tab1 =  new Tab1tests();
                return tab1;
            case 1:
                Tab2calendar tab2 =  new Tab2calendar();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Adicionar Testes";
            case 1:
                return "Mapa de Testes";
        }
        return null;
    }
}

编辑:

答案之后:

     public class Testes extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
private Caldroid_fragment cf;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testes);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    cf = new Caldroid_fragment();



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


    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
        @Override
        public void onTabSelected(TabLayout.Tab tab){
            int position = tab.getPosition();
            switch(position){
            case 0 :{}

                case 1 : {
                    addToCalendar();
                }


            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_testes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()){
       case go_to_fazer_media:{
           if (item.isChecked())
               item.setChecked(false);
            else{
           Intent i = new Intent(Testes.this,Formas.class);
           startActivity(i);
               return true;
            }
       }
       case definicoes:{
           if (item.isChecked())
               item.setChecked(false);
           else{
              return true;
           }
       }
   }


    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){

            case 0:
                Tab1tests tab1 =  new Tab1tests();
                return tab1;
            case 1:
                Tab2calendar tab2 =  new Tab2calendar();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Adicionar Testes";
            case 1:
                return "Mapa de Testes";
        }
        return null;
    }
}

}

1 个答案:

答案 0 :(得分:2)

如果您使用的是TabLayout,请使用OnTabSelectedListener

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
    int position = tab.getPosition();
    // Execute here based on position 
    switch(position){
        case 0 :

        break;
        case 2 : 
        addToCalendar();
        break;         
   }
});

标签位置从0开始,而不是1。