How to assign an Activity to open by clicking on the Actionbar Tab?

Asked

Viewed 374 times

8

Context

I have an application that contains a Activity calling for MapActivity (of which is a Map activity you inherit from the Googlemaps api), and another Activity which will serve as a search with filters to refine the results that will appear on Activity of the map.

My application has the minimum api version(minSdkVersion) as: 15 and has as 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 only put the part inside the onCreate() which I believe is where I can manipulate this so-called activity.

Code of the Pageadapter

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 point in itself is that I can’t assign a TabListener to the Tab of ActionBar, the way I’m doing it is this:

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

The problem is in new TabListener() that the compiler cannot identify nor suggest to change by another or to import some kind of library.

Detail is that I don’t know if this way is correct, and if it’s not, I’d like to know: by which way I could assign an activity correctly to the Tab of my ActionBar?

  • Make sure all the Imports related to Actionbar and Fragment refer to android.support.v7.app. For example import android.support.v7.app.ActionBar.Tab; and not import android.app.ActionBar.Tab;

  • Yes, the ActionBar and the ActionBarActivity are coming from android.support.v7.app but the ActionBar.Tab is not imported in my application

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 the ) after R.layout.activity_map and put it before the ;

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

The argument going to setTabListener() has to be a class that implements the tablistener interface

You will have to write this class. Don’t call it TabListener so that it does not get confused with the interface.

On the other hand its Activity already implements this interface, pass this as it did in the case 0:. I confess that I did not fully understand what I intended to do.

Note: With the emergence of Android 5 as Actionbar tabs are now considered deprecated. Right now the way to implement tabs is by using Tablayout, see here as.

  • how should I implement this class? I can’t imagine how

  • I confess I don’t fully understand what you want to do.

  • I just want my tabs to change activity, that’s all.

  • Thank you for warning that the closing of the parentheses was incorrect, but likewise it was not possible, I still have the same problem. But I edited in my question.

  • You already implement the interfaceTablistener in his Activity, should therefore, in the setTabListener() of case 1:, pass by this also. In the implementation of the interface you must use your Pageradapter to switch between Fragments

  • sorry, I could not understand. I could complement your answer with a solution?

  • The implementation of any solution depends on what you want to do. Put as a response the way to use Fragments, Pageradapter Tablistener, etc is almost like writing a book on the subject:).

  • But I really don’t know what to do I just know it’s not working. That’s the problem. I just want it to work.

  • Post the code of Pageadapter

  • posted, edited the question

  • Is that code yours or have you picked it up here? This post does not help anything. Post the class code Placeholderfragment

  • I just edited the question with him, The code was generated by Androidstudio itself, when I clicked New -> Activity -> Tabbed Activity -> Navigation style: Action Bar Tabs

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.