I have an app with 4 actionbar's tabs. Although I imported android.support.v7.app. ActionBar library, it's only displayed in API 16 device but not in API 10 device. What's the solution here?
Main Activity:
import android.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
public android.support.v7.app.ActionBar actionbar;
public ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getSupportActionBar();
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewpager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
addPageChangeListener();
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
viewpager.setAdapter(fragmentPagerAdapter);
addActionBarListener();
}
public void addPageChangeListener() {
ViewPager.OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionbar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
};
viewpager.setOnPageChangeListener(pageChangeListener);
}
public void addActionBarListener() {
android.support.v7.app.ActionBar.TabListener listener = new TabListener() {
@Override
public void onTabUnselected(
android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
}
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction arg1) {
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(
android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
}
};
android.support.v7.app.ActionBar.Tab tab = actionbar.newTab()
.setTabListener(listener);
tab.setCustomView(R.layout.search);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.filter);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.favorite);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.info);
actionbar.addTab(tab);
}
}
MyPagerFragmentAdapter:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public final int PAGE_COUNT = 4;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
switch (i) {
case 0:
SearchFragment searchFragment = new SearchFragment();
return searchFragment;
case 1:
FilterFragment filterFragment = new FilterFragment();
return filterFragment;
case 2:
FavoriteFragment favoriteFragment = new FavoriteFragment();
return favoriteFragment;
case 3:
InfoFragment infoFragment = new InfoFragment();
return infoFragment;
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
Main Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…