Android操作栏标签

时间:2014-06-30 09:43:22

标签: android android-fragments

我正在App中实现自定义操作栏。为此我使用支持库v7,因为我也支持android Api 10。这是My Activity

的代码
public class MainActivity extends ActionBarActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListLeft;
    private ListView mDrawerListRight;

    private ActionBarDrawerToggle mDrawerToggle;

    private ArrayList<NavDrawerItem> navDrawerItemsLeft;
    private ArrayList<NavDrawerItem> navDrawerItemsRight;
    private NavDrawerListAdapter adapterLeftDrawer;
    private NavDrawerListAdapter adapterRightDrawer;

    public static ActionBar myCustomActionBar;

    ImageButton imgLeftMenu;
    ImageButton imgRightMenu;

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

        LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.header, null);

        getSupportActionBar().setHomeButtonEnabled(true);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        getSupportActionBar().setDisplayUseLogoEnabled(false);

        getSupportActionBar().setDisplayShowCustomEnabled(true);

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#93b636")));

        getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        getSupportActionBar().setCustomView(v);

        myCustomActionBar = getSupportActionBar();
    }
}

现在问题是我想将Action Bar选项卡添加到该片段的同一个Action栏中。这是我的片段代码。

public class Shops extends Fragment implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = new String[] {"Tab 1", "Tab 2", "Tab 3", "Tab 4"};
    public Shops(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_shop, container, false);

        viewPager = (ViewPager) rootView.findViewById(R.id.pager);
        actionBar = MainActivity.myCustomActionBar;
        mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        return rootView;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        viewPager.setCurrentItem(tab.getPosition());        
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
}

它给了我这行错误

actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

这是错误日志

java.lang.IllegalStateException:操作栏选项卡必须有回调

任何帮助都会得到赞赏

1 个答案:

答案 0 :(得分:0)

检查您的进口商品。您必须导入

import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;