API 10中的Actionbar选项卡

时间:2014-06-23 13:15:26

标签: android tabs navigation android-actionbar

我有一个带有4个操作栏标签的应用。虽然我导入了android.support.v7.app。 ActionBar库,它只在API 16设备中显示,但在API 10设备中不显示。这里的解决方案是什么? 主要活动:

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;
}

}

主要活动布局:

<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" />

3 个答案:

答案 0 :(得分:1)

尝试替换import android.app.ActionBar;

使用此import android.support.v7.app.ActionBar;

另请在android:theme="@style/Theme.AppCompat"文件

中检查您的应用主题应为AndroidManifest.xml

请参阅以下链接了解更多详情......

http://wptrafficanalyzer.in/blog/swipable-navigation-tabs-using-actionbarcompat-library/

答案 1 :(得分:0)

最后,我解决了我的问题。仅仅因为我在为标签定制视图时出错。我扩展了错误的主题:

<style name="ActionBarTabStyle" parent="@android:Widget.ActionBar.TabView">

而不是:

<style name="ActionBarTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">

感谢大家支持我!

答案 2 :(得分:0)

只需关注Oficial Android ActionBar Tutorial即可。本教程将介绍如何将ActionBar与Support lib一起使用。