这是ActionBar的好用吗?

时间:2012-12-01 11:20:36

标签: java android actionbarsherlock

所以我刚开始尝试使用ActionBars,因为我想在Honeycomb之前支持设备,所以我决定去ActionBar Sherlock。我扫描了样品并想尝试一些东西。基本上我想要的是一个通用的ActionBar,它应该对所有活动都起作用。 ActionBar本身我只是用来浏览不同的活动。我很快就把一些东西砍成了一块,但不知怎的,它感觉“很脏”,有人可以看看并建议更好的实施,或指出我可能正在做的任何不良做法吗?

我的主要父活动,所有其他活动都继承:

package com.example.abstest;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;


public class ActionBarActivity extends SherlockActivity implements OnNavigationListener {

    private String[] mItems = {"First", "Second", "Third"};
    protected int mCurrentItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                context, R.layout.sherlock_spinner_item, mItems);
        adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(adapter, this);
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {

        if(mCurrentItem != itemPosition) {
            Intent intent = null;

            switch(itemPosition) {

            case 0:
                intent = new Intent(this, FirstActivity.class);
                break;

            case 1:
                intent = new Intent(this, SecondActivity.class);
                break;

            case 2:
                intent = new Intent(this, ThirdActivity.class);
                break;

            }

            startActivity(intent);
        }

        return true;
    }

}

此外,我有三种类型的活动有不同的布局:

package com.example.abstest;

import android.os.Bundle;

public class FirstActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);

        mCurrentItem = 0;
        getSupportActionBar().setSelectedNavigationItem(mCurrentItem);
    }

}

基本上,我正在使用ActionBar在这些类型的活动中进行切换。有关更好实施的建议吗?

1 个答案:

答案 0 :(得分:0)

这是您方法的替代方案。使用ViewPager和Tabs

public class ActivityMain extends SherlockFragmentActivity {
    /**
     * PRIVATE ATTRIBUTES
     */
    private static final String TAG                             = ActivityMain.class.getSimpleName();
    // ihm
    private ViewPager           _viewPager;
    private TabsAdapter         _tabsAdapter;
    private LayoutInflater      _inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        // view pager
        _viewPager = new ViewPager(this);
        _viewPager.setId(R.id.pager);
        _viewPager.setOffscreenPageLimit(10);

        setContentView(_viewPager);
        _inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //
        // action bar
        ActionBar bar = getSupportActionBar();
        bar.setTitle(R.string.app_name);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //
        // onglets
        _tabsAdapter = new TabsAdapter(this, _viewPager);
        _tabsAdapter.addTab(bar.newTab().setCustomView(getTabIndicator(getString(R.string.tle_tab_home), R.drawable.ic_menu_home)), FragmentAccueil.class, null);
        _tabsAdapter.addTab(bar.newTab().setCustomView(getTabIndicator(getString(R.string.tle_tab_directory), R.drawable.tab_list_icon)), ContainerFragmentAnnuaire.class, null);
        _tabsAdapter.addTab(bar.newTab().setCustomView(getTabIndicator(getString(R.string.tle_tab_favoris), android.R.drawable.btn_star)), FragmentFavoris.class, null);

        if (savedInstanceState == null) {
            // do stuff
        }
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        switch ((int) _viewPager.getCurrentItem()) {
            case 0:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
            case 1:
                getSupportMenuInflater().inflate(R.menu.action_bar_main, menu);
                break;
            case 2:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }


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

        static final class TabInfo {
            private final Class<?>  clss;
            private final Bundle    args;

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

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

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            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);
        }

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

        }

        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
            ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
        }

        public void onPageScrollStateChanged(int state) {
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            //
            // force l'appel de onOptionMenuPrepared
            ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }


    private View getTabIndicator(String text, int drawable) {
        View indicator = _inflater.inflate(R.layout.abs_tab, null);
        ((TextView) indicator.findViewById(R.id.tab_title)).setText(text);
        ((ImageView) indicator.findViewById(R.id.tab_icon)).setImageResource(drawable);
        return indicator;
    }