在我的应用程序中实现导航抽屉

时间:2015-02-02 20:10:12

标签: android android-layout android-fragments navigation-drawer

我想在我的应用程序中实现NavigationDrawer。我目前正在使用ActionBarSherlock以下代码:

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(true);
    bar.setDisplayShowHomeEnabled(true);
    mTabsAdapter = new TabsAdapter(this, mViewPager);

    String tab_station_list = getResources().getString(
            R.string.tab_station_list);

    // @Darshak ------------
    String tab_radio_online = getResources().getString(
            R.string.tab_radio_online);
    // ---------------------

    String tab_facebook = getResources().getString(R.string.tab_facebook);
    String tab_twitter = getResources().getString(R.string.tab_twitter);
    String tab_about = getResources().getString(R.string.tab_about);

    // @Darshak ------------

    mTabsAdapter.addTab(tab_radio_online, tab_radio_online,
            FragmentPlayer.class, null);

    mTabsAdapter.addTab(tab_station_list, tab_station_list, TVDetail.class,
            null);

    mTabsAdapter.addTab(tab_facebook, tab_facebook,
            FacebookActivity.WebviewFragment.class, null);
    mTabsAdapter.addTab(tab_twitter, tab_twitter,
            TwitterActivity.WebviewFragment.class, null);
    mTabsAdapter.addTab(tab_about, tab_about,
            AboutActivity.WebviewFragment.class, null);

    displayAd = Boolean.parseBoolean(getResources().getString(
            R.string.is_display_ad));
    displayAd();
}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final SherlockFragmentActivity mContext;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        @SuppressWarnings("unused")
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(String tag, CharSequence label, Class<?> clss,
            Bundle args) {
        ActionBar.Tab tab = mContext.getSupportActionBar().newTab();
        tab.setText(label);
        tab.setTabListener(this);
        mContext.getSupportActionBar().addTab(tab);
        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mContext.getSupportActionBar().setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.actionbarsherlock.app.ActionBar.TabListener#onTabSelected(com
     * .actionbarsherlock.app.ActionBar.Tab,
     * android.support.v4.app.FragmentTransaction)
     */
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(mContext.getSupportActionBar()
                .getSelectedNavigationIndex());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.actionbarsherlock.app.ActionBar.TabListener#onTabUnselected(com
     * .actionbarsherlock.app.ActionBar.Tab,
     * android.support.v4.app.FragmentTransaction)
     */
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.actionbarsherlock.app.ActionBar.TabListener#onTabReselected(com
     * .actionbarsherlock.app.ActionBar.Tab,
     * android.support.v4.app.FragmentTransaction)
     */
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}

为了展示NavigationDrawer,我需要更改哪些内容?

另外,我应该让我的应用与Android 2.X和3.X兼容吗? 我计划提供与Lolipop的兼容性。

谢谢

1 个答案:

答案 0 :(得分:0)

首先,我建议您不要在新项目中使用ActionbarSherlock库。当代材料设计指南建议使用Toolbar而不是Actionbar

其次,有一些教程演示了应用程序中NavigationDrawer的实现。我个人会推荐Google发布的here教程。请注意,NavigationDrawer应显示在Toolbar上方。

第三,我建议您不要为Android 2.X或3.X提供支持,除非您有时间这样做。在编写本文时,运行Android版本低于4.0的Android设备占市场的比例不到8%。此百分比将随着时间的推移而下降,因此您必须判断是否值得支持这些设备。您可以找到当前的Android版本统计信息here

祝你好运,编码愉快!