How do I assign an Activity to open when I click on the ActionBar Tab?

8

Context

I have an application that contains a Activity called MapActivity (of which is a Map type activity that inherits from GoogleMaps api), and another Activity common that will serve as a search with filters to refine the results that will appear in Activity of the map.

  

My application has the minimum api version ( minSdkVersion ) as: 15 and has the target api version ( SdkTarget ) as: 22 (current)

Code

public class MapaFiltrosActivity extends ActionBarActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

            switch (i){
                case 0: actionBar.addTab(actionBar.newTab()
                                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                                        .setTabListener(this)
                                        ); break;

                case 1: actionBar.addTab(
                        actionBar.newTab()
                                .setText(mSectionsPagerAdapter.getPageTitle(i))
                                .setTabListener(new TabListener<MapFragment>(R.layout.activity_map, this, "Mapa", MapActivity.class))
                                ); break;
            }
        }
    }
}

In the code above, I just put the part that is inside the onCreate() that I believe is where I can manipulate this activity call.

PageAdapter Code

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

Doubt

The question itself is that I can not assign a TabListener to Tab of ActionBar , the way I'm doing it is as follows:

actionBar.addTab(
    actionBar.newTab()
    .setText(mSectionsPagerAdapter.getPageTitle(i))
    .setTabListener(
        new TabListener<MapFragment>(R.layout.activity_map, this, "Mapa", MapActivity.class))
);

The problem is with% w_that the compiler can not identify and can not suggest to change for another or to import some kind of library.

Detail is that I do not know if this way is correct, and if it is not, I would like to know: By what way could I assign an activity correctly to new TabListener() of my Tab ?

    
asked by anonymous 20.07.2015 / 16:16

1 answer

2

This part of the code is not correct:

actionBar.addTab(
    actionBar.newTab()
    .setText(mSectionsPagerAdapter.getPageTitle(i))
    .setTabListener(
        new TabListener<MapFragment>(R.layout.activity_map), this, "Mapa", MapActivity.class)
);

The method setTabListener() accepts only one argument and you are passing 4.

Remove% w / o% following w /% w / o% and place w /

actionBar.addTab(
    actionBar.newTab()
    .setText(mSectionsPagerAdapter.getPageTitle(i))
    .setTabListener(
        new TabListener<MapFragment>(R.layout.activity_map, this, "Mapa", MapActivity.class)
));

The argument to ) must be a class that implements the TabListener interface

You will have to write this class. Do not call R.layout.activity_map so you do not get confused with the interface.

On the other hand your activity already implements this interface, pass ; as it did in setTabListener() . I confess I did not fully understand what I wanted to do.

Note: With the emergence of Android 5 the ActionBar tabs are now considered deprecated . At this point the way to implement tabs is using TabLayout , see here as.

    
20.07.2015 / 17:49