2

所以我刚开始尝试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 在其中 3 种类型的活动之间切换。有什么更好的实施建议吗?

4

1 回答 1

0

这是您方法的替代方法。使用 ViewPager 和选项卡

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;
    }
于 2013-07-08T08:51:11.903 回答