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 notimport android.app.ActionBar.Tab;
– ramaral
Yes, the
ActionBar
and theActionBarActivity
are coming fromandroid.support.v7.app
but theActionBar.Tab
is not imported in my application– Paulo Roberto Rosa